Forum Discussion
Previous_Value_as per Day_range
- 1 year ago
Prev_Value =
VAR CurrentYear = SELECTEDVALUE('YourTable'[Year])
VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month_Number])
VAR CurrentCustom = SELECTEDVALUE('YourTable'[Custom])RETURN
CALCULATE(
[Avg_Value],
TOPN(
1,
FILTER(
ALLSELECTED('YourTable'),
('YourTable'[Year] = CurrentYear && 'YourTable'[Month_Number] = CurrentMonth && 'YourTable'[Custom] < CurrentCustom) ||
('YourTable'[Year] = CurrentYear && 'YourTable'[Month_Number] < CurrentMonth) ||
('YourTable'[Year] < CurrentYear)
),
'YourTable'[Year], DESC,
'YourTable'[Month_Number], DESC,
'YourTable'[Custom], DESC
)
)
Hii MHTANK
You can achieve this in Power Query (M Language) by following these steps:
Solution Approach
- Group the data by Year, Month, and Day_Range, and calculate the average (Avg).
- Sort the grouped data by Year, Month, and Day_Range to ensure correct ordering.
- Add a column for the previous period's average (Prev) by referencing the previous row’s Avg value.
Step-by-Step Power Query Solution
- Load your data into Power Query.
- Transform the data using the following M code:
let
// Load the data
Source = YourTable,
// Group by Year, Month, and Day_Range and calculate the average value
GroupedData = Table.Group(Source, {"Year", "Month", "Day_Range"},
{{"Avg", each List.Average([Value]), type number}}),
// Sort the table by Year, Month, and Day_Range
SortedData = Table.Sort(GroupedData, {{"Year", Order.Ascending}, {"Month", Order.Ascending}, {"Day_Range", Order.Ascending}}),
// Add Previous column using Index and referencing the previous row
AddPrevColumn = Table.AddColumn(SortedData, "Prev",
each try SortedData[Avg]{List.PositionOf(SortedData[Avg], _)-1} otherwise null,
type nullable number
)
in
AddPrevColumnExplanation of Code
- Groups the data by Year, Month, and Day_Range and calculates the Avg (average of Value column).
- Sorts the table to ensure correct chronological order.
- Adds the Prev column by:
- Using List.PositionOf to get the previous row's Avg value.
- Using try...otherwise null to prevent errors in the first row where there's no previous value.
Expected Output
Year Month Day_Range Avg Prev
| 2024 | Nov | 1-15 | 42.00 | null |
| 2024 | Nov | 16-30 | 56.50 | 42.00 |
| 2024 | Dec | 1-15 | 50.00 | 56.50 |
| 2024 | Dec | 16-31 | 35.50 | 50.00 |
| 2025 | Jan | 1-15 | 30.83 | 35.50 |
| 2025 | Jan | 16-31 | 102.25 | 30.83 |
| 2025 | Feb | 1-15 | 82.75 | 102.25 |
| 2025 | Feb | 16-28 | 70.25 | 82.75 |
If this helps, I would appreciate your KUDOS!
Did I answer your question? Mark my post as a solution!
- MHTANK1 year ago
Helper III
Ya, this is good,
But I want this in the report matrix.- Khushidesai01091 year ago
Skilled Sharer
To achieve this in a Power BI matrix, you need to create measures instead of using Power Query. Follow these steps:Avg_Value = AVERAGE('YourTable'[Value])Create the "Prev" Measure
This measure gets the previous period's average dynamically in a matrix visualization:
Prev_Value =
VAR CurrentYear = SELECTEDVALUE('YourTable'[Year])
VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month])
VAR CurrentDayRange = SELECTEDVALUE('YourTable'[Day_Range])RETURN
CALCULATE(
[Avg_Value],
FILTER(
ALL('YourTable'),
('YourTable'[Year] = CurrentYear && 'YourTable'[Month] = CurrentMonth && 'YourTable'[Day_Range] < CurrentDayRange) ||
('YourTable'[Year] = CurrentYear && 'YourTable'[Month] < CurrentMonth) ||
('YourTable'[Year] < CurrentYear)
),
LASTNONBLANK('YourTable'[Day_Range], [Avg_Value])
)Add to the Matrix Visualization
- Rows: Year, Month, Day_Range
- Values: Avg_Value, Prev_Value
- MHTANK1 year ago
Helper III
Day_Range and Month are in "Text" data type. How "<" posible for that?