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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch 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
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!

November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.