Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon'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.
Hello together,
I am fairly new to Power Query. Maybe one of you can help me. I have used Power Query to create a table like this from different sources.
partnumber | date | source | state |
61020011 | 20.01.2023 | 0 | |
61020012 | 20.01.2023 | 0 | |
61020013 | 20.01.2023 | 0 | |
61020014 | 20.01.2023 | 0 | |
61020015 | 20.01.2023 | 0 | |
61020011 | 19.01.2023 | 0 | |
61020012 | 19.01.2023 | 0 | |
61020013 | 19.01.2023 | 0 | |
61020014 | 19.01.2023 | 0 | |
61020015 | 19.01.2023 | 0 | |
61020011 | 18.01.2023 | 2023-01-18_Längsträger LI.xlsx | ok |
61020012 | 18.01.2023 | 0 | |
61020013 | 18.01.2023 | 2023-01-18_Längsträger LI.xlsx | not ok |
61020014 | 18.01.2023 | 2023-01-18_Längsträger LI.xlsx | not ok |
61020015 | 18.01.2023 | 2023-01-18_Längsträger LI.xlsx | not ok |
61020011 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | not ok |
61020012 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | ok |
61020013 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | not ok |
61020014 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | not ok |
61020015 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | not ok |
61020011 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok |
61020012 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok |
61020013 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok |
61020014 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok |
61020015 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok |
61020011 | 15.01.2023 | 0 | |
61020012 | 15.01.2023 | 0 | |
61020013 | 15.01.2023 | 0 | |
61020014 | 15.01.2023 | 0 | |
61020015 | 15.01.2023 | 0 |
Now I want to create a column where for the unchecked dates the state of the last checked date for the part is entered. Additionally the last checked date should be added.
partnumber | date | source | state | state2 | last checked |
61020011 | 20.01.2023 | 0 | (ok) | 18.01.2023 | |
61020012 | 20.01.2023 | 0 | (ok) | 17.01.2023 | |
61020013 | 20.01.2023 | 0 | (not ok) | 18.01.2023 | |
61020014 | 20.01.2023 | 0 | (not ok) | 18.01.2023 | |
61020015 | 20.01.2023 | 0 | (not ok) | 18.01.2023 | |
61020011 | 19.01.2023 | 0 | (ok) | 18.01.2023 | |
61020012 | 19.01.2023 | 0 | (ok) | 17.01.2023 | |
61020013 | 19.01.2023 | 0 | (not ok) | 18.01.2023 | |
61020014 | 19.01.2023 | 0 | (not ok) | 18.01.2023 | |
61020015 | 19.01.2023 | 0 | (not ok) | 18.01.2023 | |
61020011 | 18.01.2023 | 2023-01-18_Längsträger LI.xlsx | ok | ok | null |
61020012 | 18.01.2023 | 0 | (ok) | 17.01.2023 | |
61020013 | 18.01.2023 | 2023-01-18_Längsträger LI.xlsx | not ok | not ok | null |
61020014 | 18.01.2023 | 2023-01-18_Längsträger LI.xlsx | not ok | not ok | null |
61020015 | 18.01.2023 | 2023-01-18_Längsträger LI.xlsx | not ok | not ok | null |
61020011 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | not ok | not ok | null |
61020012 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | ok | ok | null |
61020013 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | not ok | not ok | null |
61020014 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | not ok | not ok | null |
61020015 | 17.01.2023 | 2023-01-17_Längsträger LI.xlsx | not ok | not ok | null |
61020011 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok | not ok | null |
61020012 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok | not ok | null |
61020013 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok | not ok | null |
61020014 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok | not ok | null |
61020015 | 16.01.2023 | 2023-01-16_Längsträger LI.xlsx | not ok | not ok | null |
61020011 | 15.01.2023 | 0 | 0 | null | |
61020012 | 15.01.2023 | 0 | 0 | null | |
61020013 | 15.01.2023 | 0 | 0 | null | |
61020014 | 15.01.2023 | 0 | 0 | null | |
61020015 | 15.01.2023 | 0 | 0 | null |
I hope you get my problem.
I already tried to solve it with VBA, but my dataset will be more than two million rows. So I would like to calculate it in Power Query or Power Pivot.
Thanks in advance for your help
Warm greetings
Stephan
Solved! Go to Solution.
let
Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
Custom1 = let
group=Table.Group(Source,"partnumber",{"n",each Table.Skip(Table.Sort(_,{"date",1}),each [state]=0){0}?})
in Table.FromRecords(
Table.TransformRows(
Source,
each let
a=group{[partnumber=[partnumber]]}[n]??[],
b=if [state]<>0 or a=[] or a[date]>=[date] then [] else a
in _&[state2=b[state]? ??[state],last checked=b[date]?]
)
)
in
Custom1
Hi @SteveMueckschel ,
I believe its because in your example, when there is nothing in the source column, you have null values, whereas in mine, it was just an empty cell so every reference to [source]="" or [source] <>"" isnt working.
To make it work, amend your query to the following:
let
Source = Excel.Workbook(File.Contents("C:\Users\SMueckschel\Desktop\Test_Lösung.xlsx"), null, true),
Table1_Table = Source{[Item="Tabelle1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"partnumber", Int64.Type}, {"date", type nullable text meta [Serialized.Text = true]}, {"source", type nullable text meta [Serialized.Text = true]}, {"state", type nullable text meta [Serialized.Text = true]}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"partnumber"}, {{"maxdate", each List.Max([date]), type nullable date}, {"maxdatechecked", each List.Max(Table.SelectRows(_, each [source]<>null)[date]), type date}, {"AllRows", each _, type table [partnumber=nullable text, date=nullable date, source=nullable text, state=nullable text, Custom=number]}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"maxdate"}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Columns", "AllRows", {"date", "source", "state"}, {"date", "source", "state"}),
#"Custom1" = Table.AddColumn(#"Expanded AllRows", "state2", each if ([date] >= [maxdatechecked]) and [source] = null then "(" & List.Max(Table.SelectRows(#"Expanded AllRows", each [date]=[maxdatechecked])[state]) & ")" else [state] ),
#"Added Conditional Column" = Table.AddColumn(Custom1, "lastchecked", each if [maxdatechecked] < [date] and [source] = null then [maxdatechecked] else null),
#"Removed Columns1" = Table.RemoveColumns(#"Added Conditional Column",{"maxdatechecked"})
in
#"Removed Columns1"
Hi @m_alireza ,
sorry for my late respond.
I just noticed that there is still a mistake in your code. The last status of the certain part should always be taken over. In your code for some reason always "ok" is taken over.
But I have no idea what could be the reason for that.
Could you look again over "Custom 1".
Thanks in advance.
Greetings
Stephan
let
Source = Excel.Workbook(File.Contents("C:\Users\SMueckschel\Desktop\Test_Lösung.xlsx"), null, true),
Table1_Table = Source{[Item="Tabelle1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"partnumber", Int64.Type}, {"date", type date}, {"source", type text}, {"state", type any}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"partnumber"}, {{"maxdate", each List.Max([date]), type nullable date}, {"maxdatechecked", each List.Max(Table.SelectRows(_, each [source]<>null)[date]), type date}, {"AllRows", each _, type table [partnumber=nullable text, date=nullable date, source=nullable text, state=nullable text, Custom=number]}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"maxdate"}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Columns", "AllRows", {"date", "source", "state"}, {"date", "source", "state"}),
#"Custom1" = Table.AddColumn(#"Expanded AllRows", "state2", each if ([date] >= [maxdatechecked]) and [source] = null then "(" & List.Max(Table.SelectRows(#"Expanded AllRows", each [date]=[maxdatechecked])[state]) & ")" else [state] ),
#"Added Conditional Column" = Table.AddColumn(Custom1, "lastchecked", each if [maxdatechecked] < [date] and [source] = null then [maxdatechecked] else null),
#"Removed Columns1" = Table.RemoveColumns(#"Added Conditional Column",{"maxdatechecked"})
in
#"Removed Columns1"
let
Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
Custom1 = let
group=Table.Group(Source,"partnumber",{"n",each Table.Skip(Table.Sort(_,{"date",1}),each [state]=0){0}?})
in Table.FromRecords(
Table.TransformRows(
Source,
each let
a=group{[partnumber=[partnumber]]}[n]??[],
b=if [state]<>0 or a=[] or a[date]>=[date] then [] else a
in _&[state2=b[state]? ??[state],last checked=b[date]?]
)
)
in
Custom1
Hi @wdx223_Daniel ,
Sorry for my late respond.
Thanks for the solution. It took me a while to adjust the query to my database, because I do not completely understand what it's doing. 😅
I now have the problem that it takes about 15 minutes to refresh 5000 records.
In the end my database will have more than two million rows. I am afraid that the computing power is not sufficient for this purpose.
Do you have any ideas on how to reduce the refresh time?
Greethings
Stephan
Hi @SteveMueckschel ,
Please copy and paste this query in your advanced editor and adjust as needed to match your database:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZRBCoMwEEWvIllrmEky0R6h4A1EuiouWhTUhQfyJl5MzaJUKg7agUAIPN78+YsUhfIIBgBRxcqABtQGjF0eywFVxh/AcIDlAMcBxAFrSLwxIQ8BywGOA4gDQsjsC1ivBDDB7JFPY111fTuN1bON8rse3t2wIM3rZ4uM2+LkiLrpo+0YJ2IhEUsoLd2zpCcs5oJla7AiOZyIhUQsoVm/Z/Fnm/3bYkUsTsRCIpbQLnE/0hFgOcBxAF0Eyhk=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [partnumber = _t, date = _t, source = _t, state = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"partnumber"}, {{"maxdate", each List.Max([date]), type nullable date}, {"maxdatechecked", each List.Max(Table.SelectRows(_, each [source]<>"")[date]), type date}, {"AllRows", each _, type table [partnumber=nullable text, date=nullable date, source=nullable text, state=nullable text, Custom=number]}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"maxdate"}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Columns", "AllRows", {"date", "source", "state"}, {"date", "source", "state"}),
Custom1 = Table.AddColumn(#"Expanded AllRows", "state2", each if ([date] >= [maxdatechecked]) and [source] = "" then "(" & List.Max(Table.SelectRows(#"Expanded AllRows", each [date]=[maxdatechecked])[state]) & ")" else [state] ),
#"Added Conditional Column" = Table.AddColumn(Custom1, "lastchecked", each if [maxdatechecked] < [date] and [source] = "" then [maxdatechecked] else null),
#"Removed Columns1" = Table.RemoveColumns(#"Added Conditional Column",{"maxdatechecked"})
in
#"Removed Columns1"
Sample output (grouped by partnumber):
Hi @m_alireza ,
thank you so much for helping me with my problem.
I copied your query but it does not work properly. Maby you can show me my mistake.
This is my code.
let
Source = Excel.Workbook(File.Contents("C:\Users\SMueckschel\Desktop\Test_Lösung.xlsx"), null, true),
Table1_Table = Source{[Item="Tabelle1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"partnumber", Int64.Type}, {"date", type nullable text meta [Serialized.Text = true]}, {"source", type nullable text meta [Serialized.Text = true]}, {"state", type nullable text meta [Serialized.Text = true]}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"partnumber"}, {{"maxdate", each List.Max([date]), type nullable date}, {"maxdatechecked", each List.Max(Table.SelectRows(_, each [source]<>"")[date]), type date}, {"AllRows", each _, type table [partnumber=nullable text, date=nullable date, source=nullable text, state=nullable text, Custom=number]}}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"maxdate"}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Columns", "AllRows", {"date", "source", "state"}, {"date", "source", "state"}),
#"Custom1" = Table.AddColumn(#"Expanded AllRows", "state2", each if ([date] >= [maxdatechecked]) and [source] = "" then "(" & List.Max(Table.SelectRows(#"Expanded AllRows", each [date]=[maxdatechecked])[state]) & ")" else [state] ),
#"Added Conditional Column" = Table.AddColumn(Custom1, "lastchecked", each if [maxdatechecked] < [date] and [source] = "" then [maxdatechecked] else null),
#"Removed Columns1" = Table.RemoveColumns(#"Added Conditional Column",{"maxdatechecked"})
in
#"Removed Columns1"
as you see in this screenshot it does not work properly:
I guess its because the maxdatachecked is not working, but I can't figure out why the wrong date is filtered.
is it because of a wrong datatype?
Thank you for your help
Greethings
Stephan
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
14 | |
13 | |
12 | |
12 | |
12 |