Forum Discussion
Replicating Power Query Formula in DAX
- 1 year ago
Hi villa1980,
To replicate the previous and next row functionality in DAX, you can use DAX functions like EARLIER, and FILTER to create calculated columns for both the previous and next rows.
Here’s how you can do it in DAX:
Step 1: Create a Calculated Column for the Previous Row
This column will get the name of the previous row based on the Index column.
PreviousRow = VAR CurrentIndex = YourTable[Index] RETURN IF ( CurrentIndex = 1, BLANK(), CALCULATE( MAX(YourTable[Name]), FILTER( YourTable, YourTable[Index] = CurrentIndex - 1 ) ) )
Step 2: Create a Calculated Column for the Next Row
Similarly, you can create a calculated column to get the name of the next row.
NextRow = VAR CurrentIndex = YourTable[Index] RETURN CALCULATE( MAX(YourTable[Name]), FILTER( YourTable, YourTable[Index] = CurrentIndex + 1 ) )
Example Output
With this setup, your table might look like this:
Name Index PreviousRow NextRow
Jeff 1 BLANK() Bob Bob 2 Jeff Ian Ian 3 Bob BLANK()
Blimey, that was quick and worked really well, thank-you.
I have now tried this with end time, however, the outcome when looking at time comes back as
0.56,0.60
rather than 13:30:00, 14:30:00
I think a format would have to be placed in the filter, but not sure where I place it
The newly created column is type number. You can just change the data type to time as adding a FORMAT formula in the calc coumn returns a text. Take note, time format cannot be 24 hours and more which means 1.5 will be returned as 12pm and not one day and 12 hours.
- danextian1 year agoSuper User
If this solves, please mark this as the solution.