Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
RyanVSS
Frequent Visitor

find response of "better" or "worse" where "NA" or "No Change" present in latest record - POWERBI

RyanVSS_1-1720692736888.png

 

 

I have a mind block for coming up with a creative solution to my problem. Essentially, i have clients that report to me on session outcomes throughout the life of their account. The feedback can be NA, better, slightly better, worse, slightly worse, no change. from this feedback i need to find out the most recent outcome of better, slightly better, worse or slightly worse. this is used to indicate if the client is currenty positive or negative. 

 

therefore, i need to create a PowerBI table whereby if latest submission has the feedback of "NA" or "No "Change", then loop through previous sessions (most recent first) until a non-NA and non-No Change answer was provided. A new record should be created in the table which combines the latest feedback that's non-NA and non-No Change

 

Desired output

Regarding Obect ID: unique client 

Actual End: Latest session date

Outcome: Latest non-NA and non-No Change

 Outcome: Latest non-NA and non-No Change

Outcome: Latest non-NA and non-No Change

 Outcome: Latest non-NA and non-No Change 

 

Example:

 

RyanVSS_2-1720693068051.png

 

 

2 ACCEPTED SOLUTIONS
ryan_mayu
Super User
Super User

you can do this in PQ

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7JTM0rMVTSUTIyMDLRN9E3NAKy/RzRiVgdNLVG+sb6xgYwFeX5RcWpQDoptaQktQinFiN9MyAzHF0xTCA4JzM9oySnUsEJIoMwwQhigjHQBEOwpfkKzhmJeempqO6Ei2LoNNK30DcyRVGNbhuao+EaTSFhAleFqQ/mfpizYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Regarding ObjectID" = _t, LastestSession = _t, Outcome1 = _t, Outcome2 = _t, Outcome3 = _t, Outcome4 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Regarding ObjectID", type text}, {"LastestSession", type date}, {"Outcome1", type text}, {"Outcome2", type text}, {"Outcome3", type text}, {"Outcome4", type text}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Regarding ObjectID", Order.Ascending}, {"LastestSession", Order.Descending}}),
#"Replaced Value" = Table.ReplaceValue(#"Sorted Rows","NA",null,Replacer.ReplaceValue,{"Outcome1", "Outcome2", "Outcome3", "Outcome4"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","No Change",null,Replacer.ReplaceValue,{"Outcome1", "Outcome2", "Outcome3", "Outcome4"}),
#"Filled Up" = Table.FillUp(#"Replaced Value1",{"Outcome1", "Outcome2", "Outcome3", "Outcome4"}),
Custom1 = Table.AddColumn(#"Filled Up","Rank",each Table.RowCount(Table.SelectRows(#"Filled Up",(x)=>x[LastestSession]>[LastestSession] and x[Regarding ObjectID]=[Regarding ObjectID]))+1),
#"Filtered Rows" = Table.SelectRows(Custom1, each ([Rank] = 1)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Rank"})
in
#"Removed Columns"

11.PNG

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

@RyanVSS 

i update the sample data due to your real situation

 

11.PNG

 

pls try this

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7JTM0rMVTSUTIyMDLRN9E3NAKy/RzRiVgdNLVG+sb6xgYwFeX5RcWpQDoptaQktQinFiN9M5gOuMpwqNbgnMz0jJKcSgUniAxCuxFEuzFQuyHYxnwF54zEvPRUVEfCRTF0Gulb6BuZoqhGtw3NxXCNppAAgavC1AdzP8zZsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Regarding ObjectID" = _t, LastestSession = _t, Outcome1 = _t, Outcome2 = _t, Outcome3 = _t, Outcome4 = _t]),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Regarding ObjectID", "LastestSession"}, "Attribute", "Value"),
#"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"LastestSession", type date}}),
#"Filtered Rows1" = Table.SelectRows(#"Changed Type", each ([Value] <> "NA")),
Custom1 = Table.AddColumn( #"Filtered Rows1","Rank",each Table.RowCount(Table.SelectRows( #"Filtered Rows1",(x)=>x[LastestSession]<[LastestSession] and x[Regarding ObjectID]=[Regarding ObjectID] and x[Attribute]=[Attribute]))+1),
#"Filtered Rows" = Table.SelectRows(Custom1, each ([Rank] = 1)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Rank", "LastestSession"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Value"),
#"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"Regarding ObjectID", "Outcome1", "Outcome2", "Outcome3", "Outcome4"})
in
#"Reordered Columns"

 

12.PNG

 

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

4 REPLIES 4
RyanVSS
Frequent Visitor

hi - sorry to come back again - firstly, the solution worked as intended for sample size data, but when i apply the solution to actual dataset the model fails to process as a dataflow on the PBI service. i get the error "failed to analyze" at the initial stage when checking for errors. 

 

 

RyanVSS_0-1721670822707.png

 

when i break it down on PBI desktop i notice that the model trips up when pivoting/unpivoting the data. im not sure if there is any way around this?  I have also tried to break the script into parts and process as linked model, but it doesn't work because of the Pivot/Unpivot functions.   

 

 

 
RyanVSS
Frequent Visitor

thank you for the solution and the pbix to help me understand the solution. i have a followup question for those clients where there are "NA" or "Not Recorded" for all sessions, how do i make sure that the fill up leaves those client answers as null or "NA"/"Not Recorded" and not fill up from a previous client down the list? 

 

RyanVSS_0-1721307516448.png

 

 

client 1 fill up should be 1 - worse, 2 - null, 3 - null, 4 - null 

 

however, the fill up has taken answers from previous clients

 

 

RyanVSS_1-1721307585564.png

 

 

 

@RyanVSS 

i update the sample data due to your real situation

 

11.PNG

 

pls try this

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7JTM0rMVTSUTIyMDLRN9E3NAKy/RzRiVgdNLVG+sb6xgYwFeX5RcWpQDoptaQktQinFiN9M5gOuMpwqNbgnMz0jJKcSgUniAxCuxFEuzFQuyHYxnwF54zEvPRUVEfCRTF0Gulb6BuZoqhGtw3NxXCNppAAgavC1AdzP8zZsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Regarding ObjectID" = _t, LastestSession = _t, Outcome1 = _t, Outcome2 = _t, Outcome3 = _t, Outcome4 = _t]),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Regarding ObjectID", "LastestSession"}, "Attribute", "Value"),
#"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"LastestSession", type date}}),
#"Filtered Rows1" = Table.SelectRows(#"Changed Type", each ([Value] <> "NA")),
Custom1 = Table.AddColumn( #"Filtered Rows1","Rank",each Table.RowCount(Table.SelectRows( #"Filtered Rows1",(x)=>x[LastestSession]<[LastestSession] and x[Regarding ObjectID]=[Regarding ObjectID] and x[Attribute]=[Attribute]))+1),
#"Filtered Rows" = Table.SelectRows(Custom1, each ([Rank] = 1)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Rank", "LastestSession"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Value"),
#"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"Regarding ObjectID", "Outcome1", "Outcome2", "Outcome3", "Outcome4"})
in
#"Reordered Columns"

 

12.PNG

 

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




ryan_mayu
Super User
Super User

you can do this in PQ

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7JTM0rMVTSUTIyMDLRN9E3NAKy/RzRiVgdNLVG+sb6xgYwFeX5RcWpQDoptaQktQinFiN9MyAzHF0xTCA4JzM9oySnUsEJIoMwwQhigjHQBEOwpfkKzhmJeempqO6Ei2LoNNK30DcyRVGNbhuao+EaTSFhAleFqQ/mfpizYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Regarding ObjectID" = _t, LastestSession = _t, Outcome1 = _t, Outcome2 = _t, Outcome3 = _t, Outcome4 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Regarding ObjectID", type text}, {"LastestSession", type date}, {"Outcome1", type text}, {"Outcome2", type text}, {"Outcome3", type text}, {"Outcome4", type text}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Regarding ObjectID", Order.Ascending}, {"LastestSession", Order.Descending}}),
#"Replaced Value" = Table.ReplaceValue(#"Sorted Rows","NA",null,Replacer.ReplaceValue,{"Outcome1", "Outcome2", "Outcome3", "Outcome4"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","No Change",null,Replacer.ReplaceValue,{"Outcome1", "Outcome2", "Outcome3", "Outcome4"}),
#"Filled Up" = Table.FillUp(#"Replaced Value1",{"Outcome1", "Outcome2", "Outcome3", "Outcome4"}),
Custom1 = Table.AddColumn(#"Filled Up","Rank",each Table.RowCount(Table.SelectRows(#"Filled Up",(x)=>x[LastestSession]>[LastestSession] and x[Regarding ObjectID]=[Regarding ObjectID]))+1),
#"Filtered Rows" = Table.SelectRows(Custom1, each ([Rank] = 1)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Rank"})
in
#"Removed Columns"

11.PNG

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.