Forum Discussion
Previous_Value_as per Day_range
| Date | Year | Month | Day_Range | Value |
| 09-11-2024 | 2024 | Nov | 1-15 | 41.00 |
| 15-11-2024 | 2024 | Nov | 1-15 | 43.00 |
| 25-11-2024 | 2024 | Nov | 16-30 | 55.00 |
| 29-11-2024 | 2024 | Nov | 16-30 | 58.00 |
| 03-12-2024 | 2024 | Dec | 1-15 | 85.00 |
| 06-12-2024 | 2024 | Dec | 1-15 | 15.00 |
| 29-12-2024 | 2024 | Dec | 16-31 | 67.00 |
| 30-12-2024 | 2024 | Dec | 16-31 | 4.00 |
| 01-01-2025 | 2025 | Jan | 1-15 | 10.00 |
| 02-01-2025 | 2025 | Jan | 1-15 | 20.00 |
| 04-01-2025 | 2025 | Jan | 1-15 | 21.50 |
| 05-01-2025 | 2025 | Jan | 1-15 | 23.00 |
| 07-01-2025 | 2025 | Jan | 1-15 | 50.00 |
| 08-01-2025 | 2025 | Jan | 1-15 | 51.50 |
| 09-01-2025 | 2025 | Jan | 1-15 | 21.00 |
| 10-01-2025 | 2025 | Jan | 1-15 | 22.50 |
| 15-01-2025 | 2025 | Jan | 1-15 | 58.00 |
| 17-01-2025 | 2025 | Jan | 16-31 | 100.00 |
| 20-01-2025 | 2025 | Jan | 16-31 | 101.50 |
| 22-01-2025 | 2025 | Jan | 16-31 | 103.00 |
| 30-01-2025 | 2025 | Jan | 16-31 | 104.50 |
| 01-02-2025 | 2025 | Feb | 1-15 | 106.00 |
| 02-02-2025 | 2025 | Feb | 1-15 | 107.50 |
| 03-02-2025 | 2025 | Feb | 1-15 | 75.00 |
| 09-02-2025 | 2025 | Feb | 1-15 | 76.50 |
| 10-02-2025 | 2025 | Feb | 1-15 | 65.00 |
| 15-02-2025 | 2025 | Feb | 1-15 | 66.50 |
| 18-02-2025 | 2025 | Feb | 16-28 | 68.00 |
| 20-02-2025 | 2025 | Feb | 16-28 | 69.50 |
| 22-02-2025 | 2025 | Feb | 16-28 | 71.00 |
| 27-02-2025 | 2025 | Feb | 16-28 | 72.50 |
This is my Data.
| Year | Month | Day_Range | Avg | Prev |
| 2024 | Nov | 1-15 | 42.00 | |
| 16-30 | 56.50 | 42.00 | ||
| Dec | 1-15 | 50.00 | 56.50 | |
| 16-31 | 35.50 | 50.00 | ||
| 2025 | Jan | 1-15 | 30.83 | 35.50 |
| 16-31 | 102.25 | 30.83 | ||
| Feb | 1-15 | 82.75 | 102.25 | |
| 16-28 | 70.25 | 82.75 |
This is result that I want.
Here I want Previous value column. Here problem is that in 30 days month, 31 days month and 28 days month, 29 days month.
So, how i can solve this problem. Please suggest me.
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
)
)
15 Replies
- Khushidesai0109
Skilled Sharer
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!- MHTANK
Helper III
Ya, this is good,
But I want this in the report matrix.- Khushidesai0109
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