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

Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.

Reply
BenjaminSNN
Frequent Visitor

Power Query M - get value from column other table based on condition

Hello everyone,

Hopefully you can help me with the following situation in Power Query.

 

Situation:
We have projects within programs. The project can be in different phases. For each phase in a programm a maximum term (days) is set. This maximum differs between programms and phases. 

 

Table 1

Programphase1phase2phase3
A73514
B213521
C25427

 

Table 2

ProjectProgramphasestartedended
1A11-11-20227-11-2022
2A33-11-202218-11-2022
3B25-11-202224-11-2022
4C21-11-2022 
5C110-11-2022 

 

Desired outcome:

I want to know for each project the maximum term based on program and phase. Hopefully by referencing the correct column in the other table and not adding al those columns.

ProjectProgramphasestartedendedterm
1A11-11-20227-11-20227
2A33-11-202218-11-202214
3B25-11-202224-11-202235
4C21-11-2022 42
5C110-11-2022 25


Another solution would be to make a combined column of 'program - phase' in both tables. Pivot table 2 and then join it on that column. But I am hoping for a solution with 1 step/formula

Thanks in advance!!

 

1 ACCEPTED SOLUTION
ddpl
Solution Sage
Solution Sage

@BenjaminSNN ,

 

In Power Query...

 

Table 1...

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIHYmNTIGFoohSrE63kBGQaGcIEgSyQoDOICeKbGIG1xMYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Program = _t, phase1 = _t, phase2 = _t, phase3 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Program", type text}, {"phase1", Int64.Type}, {"phase2", Int64.Type}, {"phase3", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Program"}, "Attribute", "Value"),
#"Replaced Value" = Table.ReplaceValue(#"Unpivoted Other Columns","phase","",Replacer.ReplaceText,{"Attribute"}),
#"Renamed Columns" = Table.RenameColumns(#"Replaced Value",{{"Attribute", "Phase"}, {"Value", "Term"}}),
#"Merged Columns" = Table.CombineColumns(#"Renamed Columns",{"Program", "Phase"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Program-Phase")
in
#"Merged Columns"

 

Table 2...

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYhBtYKhraKhrZARimkOZsTrRSkZQNcYgCWO4GkMLJDUgOScgBms2hasxMkFSYwIUcIapQdgFljOFyoHcYWiAJBcLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Project = _t, Program = _t, phase = _t, started = _t, ended = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project", Int64.Type}, {"Program", type text}, {"phase", Int64.Type}, {"started", type date}, {"ended", type text}}),
#"Inserted Merged Column" = Table.AddColumn(#"Changed Type", "Program-Phase", each Text.Combine({[Program], Text.From([phase], "en-US")}, "-"), type text),
#"Merged Queries" = Table.NestedJoin(#"Inserted Merged Column", {"Program-Phase"}, #"Table 1", {"Program-Phase"}, "Table 1", JoinKind.LeftOuter),
#"Expanded Table 1" = Table.ExpandTableColumn(#"Merged Queries", "Table 1", {"Term"}, {"Term"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Table 1",{"Program-Phase"})
in
#"Removed Columns"

View solution in original post

1 REPLY 1
ddpl
Solution Sage
Solution Sage

@BenjaminSNN ,

 

In Power Query...

 

Table 1...

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIHYmNTIGFoohSrE63kBGQaGcIEgSyQoDOICeKbGIG1xMYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Program = _t, phase1 = _t, phase2 = _t, phase3 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Program", type text}, {"phase1", Int64.Type}, {"phase2", Int64.Type}, {"phase3", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Program"}, "Attribute", "Value"),
#"Replaced Value" = Table.ReplaceValue(#"Unpivoted Other Columns","phase","",Replacer.ReplaceText,{"Attribute"}),
#"Renamed Columns" = Table.RenameColumns(#"Replaced Value",{{"Attribute", "Phase"}, {"Value", "Term"}}),
#"Merged Columns" = Table.CombineColumns(#"Renamed Columns",{"Program", "Phase"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Program-Phase")
in
#"Merged Columns"

 

Table 2...

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYhBtYKhraKhrZARimkOZsTrRSkZQNcYgCWO4GkMLJDUgOScgBms2hasxMkFSYwIUcIapQdgFljOFyoHcYWiAJBcLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Project = _t, Program = _t, phase = _t, started = _t, ended = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project", Int64.Type}, {"Program", type text}, {"phase", Int64.Type}, {"started", type date}, {"ended", type text}}),
#"Inserted Merged Column" = Table.AddColumn(#"Changed Type", "Program-Phase", each Text.Combine({[Program], Text.From([phase], "en-US")}, "-"), type text),
#"Merged Queries" = Table.NestedJoin(#"Inserted Merged Column", {"Program-Phase"}, #"Table 1", {"Program-Phase"}, "Table 1", JoinKind.LeftOuter),
#"Expanded Table 1" = Table.ExpandTableColumn(#"Merged Queries", "Table 1", {"Term"}, {"Term"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Table 1",{"Program-Phase"})
in
#"Removed Columns"

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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