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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mgrayTCB
Helper III
Helper III

chose between sets of records based on status

I am not sure where to begin with this in power queary. I have bunch of tables that have data by project number that is either "final", "current", or both "final" and "current" records. For projects that have both final and current I alway only want final, otherwise if there is only one set for a particular project I will use the one that exists. In the example below I removed the project 1 "current" records becuase there are final records. For project 3 I use current becuase that is all that exits.

 

Any help would be appreciated.

 

Simplified data would look like this:

mgrayTCB_0-1654804090529.png

 

Input Table   
ProjIDStatusYearAmt
1final201877
1final201955
1final202065
1final202135
1current201950
1current202060
1current202170
1current201866
2final2019100
2final2020500
2final2021300
3current2019666
3current2020777
3current2021666
    
    
Output Table  
ProjIDStatusYearAmt
1final201877
1final201955
1final202065
1final202135
2final2019100
2final2020500
2final2021300
3current2019666
3current2020777
3current2021666

 

 

 

2 ACCEPTED SOLUTIONS
AlB
Super User
Super User

Hi @mgrayTCB 

Place the following M code in a blank query to see the steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddBNCoAgEAbQu7h2MaOodRZpEVEQhAup+zdORpHT6kMfzo8xKlRaLWsaN0oD2FGEoAbdSE/hnCAGKLwo5WgfmY6c57S/6oFoV8UfKxdBNp7eezbTTo8AAnEzJxPPX8lKC/ja7YtcNNSPbBDvl8MJ", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ProjID = _t, Status = _t, Year = _t, Amt = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ProjID", Int64.Type}, {"Status", type text}, {"Year", Int64.Type}, {"Amt", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ProjID"}, {{"Result", each List.Max(List.Distinct([Status])), type nullable text}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ProjID"}, #"Grouped Rows", {"ProjID"}, "merge_", JoinKind.LeftOuter),
    #"Expanded merge_" = Table.ExpandTableColumn(#"Merged Queries", "merge_", {"Result"}, {"Result"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded merge_", each ([Result] = [Status])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Result"})
in
    #"Removed Columns"

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

View solution in original post

AlB
Super User
Super User

@mgrayTCB 

And if there's nor final nor current?

Pending that clarification, here's an alternative. Note the only change is around that List.Max()  in the #"Grouped Rows" step

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddFNDoMgEAXgu7B2MaD8ncW4aIxNTYwmRO8vTFGa8lxN4Asz8Oh7IUUj3vP6WmJVJF0s1oqhqcTHojUQRbEYKGnZFhmPEKZ1/+lH0L4dHyxtWGx8e2Nu2/bPFMo074HwLOeQpGXXoW6uZKHqlCQRIB6kMXFOmVoUlMmv+kduavOHVSivk8MJ", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ProjID = _t, Status = _t, Year = _t, Amt = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ProjID", Int64.Type}, {"Status", type text}, {"Year", Int64.Type}, {"Amt", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ProjID"}, {{"Result", each if List.Contains([Status], "final") then "final" else if List.Contains([Status], "current") then "current" else null , type nullable text}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ProjID"}, #"Grouped Rows", {"ProjID"}, "merge_", JoinKind.LeftOuter),
    #"Expanded merge_" = Table.ExpandTableColumn(#"Merged Queries", "merge_", {"Result"}, {"Result"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded merge_", each ([Result] = [Status])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Result"})
in
    #"Removed Columns"

 

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

 

View solution in original post

6 REPLIES 6
AlB
Super User
Super User

@mgrayTCB 

And if there's nor final nor current?

Pending that clarification, here's an alternative. Note the only change is around that List.Max()  in the #"Grouped Rows" step

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddFNDoMgEAXgu7B2MaD8ncW4aIxNTYwmRO8vTFGa8lxN4Asz8Oh7IUUj3vP6WmJVJF0s1oqhqcTHojUQRbEYKGnZFhmPEKZ1/+lH0L4dHyxtWGx8e2Nu2/bPFMo074HwLOeQpGXXoW6uZKHqlCQRIB6kMXFOmVoUlMmv+kduavOHVSivk8MJ", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ProjID = _t, Status = _t, Year = _t, Amt = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ProjID", Int64.Type}, {"Status", type text}, {"Year", Int64.Type}, {"Amt", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ProjID"}, {{"Result", each if List.Contains([Status], "final") then "final" else if List.Contains([Status], "current") then "current" else null , type nullable text}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ProjID"}, #"Grouped Rows", {"ProjID"}, "merge_", JoinKind.LeftOuter),
    #"Expanded merge_" = Table.ExpandTableColumn(#"Merged Queries", "merge_", {"Result"}, {"Result"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded merge_", each ([Result] = [Status])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Result"})
in
    #"Removed Columns"

 

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

 

If there is no final or current I would want nothing. I guess by filtering out "other" first your orignal code would work but was looking for something more precise that max of sorted text. Something that could be adapted to explicity pull certain status tags if there are more than one.

@mgrayTCB 

I've edited my previous response. See if that does the trick


SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

AlB
Super User
Super User

Hi @mgrayTCB 

Place the following M code in a blank query to see the steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddBNCoAgEAbQu7h2MaOodRZpEVEQhAup+zdORpHT6kMfzo8xKlRaLWsaN0oD2FGEoAbdSE/hnCAGKLwo5WgfmY6c57S/6oFoV8UfKxdBNp7eezbTTo8AAnEzJxPPX8lKC/ja7YtcNNSPbBDvl8MJ", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ProjID = _t, Status = _t, Year = _t, Amt = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ProjID", Int64.Type}, {"Status", type text}, {"Year", Int64.Type}, {"Amt", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ProjID"}, {{"Result", each List.Max(List.Distinct([Status])), type nullable text}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ProjID"}, #"Grouped Rows", {"ProjID"}, "merge_", JoinKind.LeftOuter),
    #"Expanded merge_" = Table.ExpandTableColumn(#"Merged Queries", "merge_", {"Result"}, {"Result"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded merge_", each ([Result] = [Status])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Result"})
in
    #"Removed Columns"

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Hi,

Follow up problem. It turns out that my sample data was a bit too simplistic. I see you are using list.max for the status field to id the "final" instead of "current" but some of the projects also have and "other" status in that case I think list.max is getting the wrong value since "other" sorts after "final".  I tried to modify your M code to incorporate some soft of if statement but keep getting stuck. So the general logic is, if there is a final get that, otherwise get current. Always ignore other. Is there a better way to do this so I am not relying on sorting of text?

 

See revised sample data

ProjectIDStatusYearAmt
1final201877
1final201955
1final202065
1final202135
1current201950
1current202060
1current202170
1current201866
1other201999
1other202088
1other202144
1other201855
2final2019100
2final2020500
2final2021300
3current2019666
3current2020777
3current2021666

Thank you. That is a super helpful example of the logic

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors