[PQ/Power BI] - Assigning a phase from another queries date range

Dutsj

New Member
Joined
Jan 10, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
I have two queries where i can merge them with the "ID". The phase query has the phase ID, and the phase start and end date. The other query has hour values and a date value, and i want to add a column to this query with the Phase name, depending on which phase date range the date value is in. See below tables (or attached image with colours for better explanation). I'm using Power BI so i'd like to be able to do this in either Power BI or Power Query. I hope it makes sense and that someone knows how to solve this :)


Phase query
IDPhase NamePhase Start datePhase end date
1​
A
01-01-2022​
31-01-2022​
1​
B
01-02-2022​
28-02-2022​
2​
B
01-02-2022​
28-02-2022​
3​
A
01-03-2022​
20-03-2022​
3​
B
01-04-2022​
15-04-2022​

Hour queryAll i need to get my result is to add the phase here with a merge or similar
IDHoursDatePhase
1​
2​
05-01-2022​
?
1​
3​
07-02-2022​
?
2​
2​
10-02-2022​
?
2​
10​
01-01-1901​
?
3​
5​
05-03-2022​
?
3​
8​
02-04-2022​
?
3​
3​
08-04-2022​
?

Result (visualisation)
IDHours (combined)Phase
1​
2​
A
1​
3​
B
2​
2​
B
3​
5​
A
3​
11​
B
with colors.PNG
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="PhaseTable"]}[Content],
    PhaseQuery = Table.TransformColumnTypes(Source,{{"Phase Start date", type date}, {"Phase end date", type date}}),
    tbl1 = Excel.CurrentWorkbook(){[Name="HoursTable"]}[Content],
    HoursQuery = Table.TransformColumnTypes(tbl1,{{"Date", type date}}),
    AddPhaseName = Table.AddColumn(HoursQuery, "Phase Name", each 
                    Text.Combine(Table.SelectRows(PhaseQuery, (x)=> x[ID] = _[ID] and _[Date]>= x[Phase Start date] and _[Date] <= x[Phase end date])[Phase Name], ", ")),
    RemoveDateColumn = Table.RemoveColumns(AddPhaseName,{"Date"})
in
    RemoveDateColumn

Book1
ABCDEFGHI
1IDPhase NamePhase Start datePhase end date
21A1/1/20221/31/2022
31B2/1/20222/28/2022
42B1/2/20222/28/2022
53A1/3/20223/20/2022
63B1/4/20224/15/2022
7
8
9IDHoursDateIDHoursPhase Name
10121/5/202212A
11132/7/202213B
12222/10/202222B
132101/1/1901210
14353/5/202235A, B
15384/2/202238B
16334/8/202233B
17
Sheet1
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="PhaseTable"]}[Content],
    PhaseQuery = Table.TransformColumnTypes(Source,{{"Phase Start date", type date}, {"Phase end date", type date}}),
    tbl1 = Excel.CurrentWorkbook(){[Name="HoursTable"]}[Content],
    HoursQuery = Table.TransformColumnTypes(tbl1,{{"Date", type date}}),
    AddPhaseName = Table.AddColumn(HoursQuery, "Phase Name", each
                    Text.Combine(Table.SelectRows(PhaseQuery, (x)=> x[ID] = _[ID] and _[Date]>= x[Phase Start date] and _[Date] <= x[Phase end date])[Phase Name], ", ")),
    RemoveDateColumn = Table.RemoveColumns(AddPhaseName,{"Date"})
in
    RemoveDateColumn

Book1
ABCDEFGHI
1IDPhase NamePhase Start datePhase end date
21A1/1/20221/31/2022
31B2/1/20222/28/2022
42B1/2/20222/28/2022
53A1/3/20223/20/2022
63B1/4/20224/15/2022
7
8
9IDHoursDateIDHoursPhase Name
10121/5/202212A
11132/7/202213B
12222/10/202222B
132101/1/1901210
14353/5/202235A, B
15384/2/202238B
16334/8/202233B
17
Sheet1
Looks great! I had some issues with my initial setup but i fixed it, thanks. I had no idea you could have multiple tables loaded into a single query like that and then referencing them backwards, thanks!
 
Last edited:
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="PhaseTable"]}[Content],
    PhaseQuery = Table.TransformColumnTypes(Source,{{"Phase Start date", type date}, {"Phase end date", type date}}),
    tbl1 = Excel.CurrentWorkbook(){[Name="HoursTable"]}[Content],
    HoursQuery = Table.TransformColumnTypes(tbl1,{{"Date", type date}}),
    AddPhaseName = Table.AddColumn(HoursQuery, "Phase Name", each
                    Text.Combine(Table.SelectRows(PhaseQuery, (x)=> x[ID] = _[ID] and _[Date]>= x[Phase Start date] and _[Date] <= x[Phase end date])[Phase Name], ", ")),
    RemoveDateColumn = Table.RemoveColumns(AddPhaseName,{"Date"})
in
    RemoveDateColumn

Book1
ABCDEFGHI
1IDPhase NamePhase Start datePhase end date
21A1/1/20221/31/2022
31B2/1/20222/28/2022
42B1/2/20222/28/2022
53A1/3/20223/20/2022
63B1/4/20224/15/2022
7
8
9IDHoursDateIDHoursPhase Name
10121/5/202212A
11132/7/202213B
12222/10/202222B
132101/1/1901210
14353/5/202235A, B
15384/2/202238B
16334/8/202233B
17
Sheet1
I spoke too early, unfortunately my dataset is too large and it freezes when i do it with the whole dataset, i tried letting it load but gave up after an hour... Any suggestions on how to make it lighter?
 
Upvote 0
try wrapping the two main tables (in the example PhaseQuery and HoursQuery) in Table.Buffer

like this for the example:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="PhaseTable"]}[Content],
    PhaseQuery = Table.Buffer(Table.TransformColumnTypes(Source,{{"Phase Start date", type date}, {"Phase end date", type date}})),
    tbl1 = Excel.CurrentWorkbook(){[Name="HoursTable"]}[Content],
    HoursQuery = Table.Buffer(Table.TransformColumnTypes(tbl1,{{"Date", type date}})),
    AddPhaseName = Table.AddColumn(HoursQuery, "Phase Name", each 
                    Text.Combine(Table.SelectRows(PhaseQuery, (x)=> x[ID] = _[ID] and _[Date]>= x[Phase Start date] and _[Date] <= x[Phase end date])[Phase Name], ", ")),
    RemoveDateColumn = Table.RemoveColumns(AddPhaseName,{"Date"})
in
    RemoveDateColumn
 
Upvote 0
Solution
try wrapping the two main tables (in the example PhaseQuery and HoursQuery) in Table.Buffer

like this for the example:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="PhaseTable"]}[Content],
    PhaseQuery = Table.Buffer(Table.TransformColumnTypes(Source,{{"Phase Start date", type date}, {"Phase end date", type date}})),
    tbl1 = Excel.CurrentWorkbook(){[Name="HoursTable"]}[Content],
    HoursQuery = Table.Buffer(Table.TransformColumnTypes(tbl1,{{"Date", type date}})),
    AddPhaseName = Table.AddColumn(HoursQuery, "Phase Name", each
                    Text.Combine(Table.SelectRows(PhaseQuery, (x)=> x[ID] = _[ID] and _[Date]>= x[Phase Start date] and _[Date] <= x[Phase end date])[Phase Name], ", ")),
    RemoveDateColumn = Table.RemoveColumns(AddPhaseName,{"Date"})
in
    RemoveDateColumn
Amazing, thank you so much!

Does the table.buffer make sure that it does the previous calculations first and then compares the dates instead of doing the calculations everytime it compares the dates?
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top