Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
| 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.
Solved! Go to Solution.
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:
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
AddPrevColumnYear 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!
Ya, this is good,
But I want this in the report matrix.
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])
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])
)
Day_Range and Month are in "Text" data type. How "<" posible for that?
Hiii @MHTANK
Here is the modified DAX measure
Prev_Value =
VAR CurrentYear = SELECTEDVALUE('YourTable'[Year])
VAR CurrentMonth = VALUE(SELECTEDVALUE('YourTable'[Month])) -- Convert to number
VAR CurrentDayRange = VALUE(SELECTEDVALUE('YourTable'[Day_Range])) -- Convert to number
RETURN
CALCULATE(
[Avg_Value],
FILTER(
ALL('YourTable'),
('YourTable'[Year] = CurrentYear && VALUE('YourTable'[Month]) = CurrentMonth && VALUE('YourTable'[Day_Range]) < CurrentDayRange) ||
('YourTable'[Year] = CurrentYear && VALUE('YourTable'[Month]) < CurrentMonth) ||
('YourTable'[Year] < CurrentYear)
),
LASTNONBLANK('YourTable'[Day_Range], [Avg_Value])
)
If this helps, I would appreciate your KUDOS!
Did I answer your question? Mark my post as a solution!
This give me error. cannot convert "Jan" of type text to type number.
Create a Numeric Month calculated Column
Create a calculated column in your table to convert month names into numbers
Month_Number =
SWITCH(
'YourTable'[Month],
"Jan", 1, "Feb", 2, "Mar", 3, "Apr", 4, "May", 5, "Jun", 6,
"Jul", 7, "Aug", 8, "Sep", 9, "Oct", 10, "Nov", 11, "Dec", 12,
BLANK()
)
Modify Your Measure to Use Month_Number
Prev_Value =
VAR CurrentYear = SELECTEDVALUE('YourTable'[Year])
VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month_Number]) -- Use the numeric month column
VAR CurrentDayRange = VALUE(SELECTEDVALUE('YourTable'[Day_Range])) -- Ensure numeric comparison
RETURN
CALCULATE(
[Avg_Value],
FILTER(
ALL('YourTable'),
('YourTable'[Year] = CurrentYear && 'YourTable'[Month_Number] = CurrentMonth && VALUE('YourTable'[Day_Range]) < CurrentDayRange) ||
('YourTable'[Year] = CurrentYear && 'YourTable'[Month_Number] < CurrentMonth) ||
('YourTable'[Year] < CurrentYear)
),
LASTNONBLANK('YourTable'[Day_Range], [Avg_Value])
)
not working
not getting output
you getting any error?
not error. But not find desired output
Here Custom=
Umm... Okayyyy
Please Try this
Prev_Value =
VAR CurrentYear = SELECTEDVALUE('YourTable'[Year])
VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month_Number])
VAR CurrentCustom = SELECTEDVALUE('YourTable'[Custom]) -- Consider "Custom" as well
RETURN
CALCULATE(
[Avg_Value],
FILTER(
ALLSELECTED('YourTable'),
('YourTable'[Year] = CurrentYear && 'YourTable'[Month_Number] = CurrentMonth && 'YourTable'[Custom] < CurrentCustom) ||
('YourTable'[Year] = CurrentYear && 'YourTable'[Month_Number] < CurrentMonth) ||
('YourTable'[Year] < CurrentYear)
),
LASTNONBLANK('YourTable'[Custom], [Avg_Value])
)
I used same already, but didn't get
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
)
)
Ya,
It's working very well
Thank you so much👍👍👍
Keep Doing DAX!!
Kuddos are Appreciated
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 48 | |
| 45 | |
| 41 | |
| 20 | |
| 18 |
| User | Count |
|---|---|
| 69 | |
| 64 | |
| 32 | |
| 31 | |
| 27 |