Forum Discussion
Power Query - Nested loop iteration with the output in the first row and so on
Hi Anonymous
Do you must do it with Power Query? If you may accept DAX solutions, you can try my method below. If you want to do it only with Power Query, let me know then I will try to work it out.
First add a custom column with Power Query to get Month Start Date for every row.
Date.FromText("1"&[Month])
After applying the change to data model, create calculated columns with the following DAX formula.
Month WIP used in =
VAR __table =
SELECTCOLUMNS (
FILTER ( 'Table', 'Table'[YearMonth] <= EARLIER ( 'Table'[YearMonth] ) ),
"_yearMonth", 'Table'[YearMonth],
"_WIP", 'Table'[WIP],
"_fees", 'Table'[Fees]
)
VAR __firstWIP = 'Table'[WIP]
VAR __table2 =
ADDCOLUMNS (
__table,
"_runningWIP",
__firstWIP
- SUMX ( FILTER ( __table, [_yearMonth] >= EARLIER ( [_yearMonth] ) ), [_fees] )
)
RETURN
MAXX ( FILTER ( __table2, [_runningWIP] <= 0 ), [_yearMonth] )
# of months = DATEDIFF ( 'Table'[Month WIP used in], 'Table'[YearMonth], MONTH ) + 1
Running WIP =
'Table'[WIP]
- SUMX (
FILTER (
'Table',
'Table'[YearMonth] <= EARLIER ( 'Table'[YearMonth] )
&& 'Table'[YearMonth] >= EARLIER ( 'Table'[Month WIP used in] )
),
'Table'[Fees]
)
Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.
- Anonymous4 years agoNot applicable
Hi Jing,
Thank you so much for taking the time to look at this, I really appreciate it and the solution works great!
The only reason why I was concerned to use DAX instead of power query was a concern that it would have performance issues if I was needing to iterate over many rows of data. The example I provided was a small illustration but I may need to summarise and performance this calculation across a dataset that is initially thousands of rows before being transformed.
From your experience, would you expect the DAX solution to slow down performance on a large dataset?
Thank you again.James