Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Next 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

Reply
MHTANK
Helper III
Helper III

Previous_Value_as per Day_range

DateYearMonthDay_RangeValue
09-11-2024  2024Nov1-1541.00
15-11-2024  2024Nov1-1543.00
25-11-2024  2024Nov16-3055.00
29-11-2024  2024Nov16-3058.00
03-12-2024  2024Dec1-1585.00
06-12-2024  2024Dec1-1515.00
29-12-2024  2024Dec16-3167.00
30-12-2024  2024Dec16-314.00
01-01-2025  2025Jan1-1510.00
02-01-2025  2025Jan1-1520.00
04-01-2025  2025Jan1-1521.50
05-01-2025  2025Jan1-1523.00
07-01-2025  2025Jan1-1550.00
08-01-2025  2025Jan1-1551.50
09-01-2025  2025Jan1-1521.00
10-01-2025  2025Jan1-1522.50
15-01-2025  2025Jan1-1558.00
17-01-2025  2025Jan16-31100.00
20-01-2025  2025Jan16-31101.50
22-01-2025  2025Jan16-31103.00
30-01-2025  2025Jan16-31104.50
01-02-2025  2025Feb1-15106.00
02-02-2025  2025Feb1-15107.50
03-02-2025  2025Feb1-1575.00
09-02-2025  2025Feb1-1576.50
10-02-2025  2025Feb1-1565.00
15-02-2025  2025Feb1-1566.50
18-02-2025  2025Feb16-2868.00
20-02-2025  2025Feb16-2869.50
22-02-2025  2025Feb16-2871.00
27-02-2025  2025Feb16-2872.50

This is my Data.

YearMonthDay_RangeAvgPrev
2024Nov1-1542.00 
  16-3056.5042.00
 Dec1-1550.0056.50
  16-3135.5050.00
2025Jan1-1530.8335.50
  16-31102.2530.83
 Feb1-1582.75102.25
  16-2870.2582.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.

1 ACCEPTED 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
)
)

Proud to be a Super User!!

View solution in original post

15 REPLIES 15
Khushidesai0109
Super User
Super User

Hii @MHTANK 

You can achieve this in Power Query (M Language) by following these steps:

Solution Approach

  1. Group the data by Year, Month, and Day_Range, and calculate the average (Avg).
  2. Sort the grouped data by Year, Month, and Day_Range to ensure correct ordering.
  3. Add a column for the previous period's average (Prev) by referencing the previous row’s Avg value.

Step-by-Step Power Query Solution

  1. Load your data into Power Query.
  2. 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
    AddPrevColumn

Explanation of Code

  1. Groups the data by Year, Month, and Day_Range and calculates the Avg (average of Value column).
  2. Sorts the table to ensure correct chronological order.
  3. 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

2024Nov1-1542.00null
2024Nov16-3056.5042.00
2024Dec1-1550.0056.50
2024Dec16-3135.5050.00
2025Jan1-1530.8335.50
2025Jan16-31102.2530.83
2025Feb1-1582.75102.25
2025Feb16-2870.2582.75



If this helps, I would appreciate your KUDOS!
Did I answer your question? Mark my post as a solution!

Proud to be a Super User!!

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])


 

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



@MHTANK

Proud to be a Super User!!

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!

Proud to be a Super User!!

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])
)

 

Proud to be a Super User!!

not working
not getting output

you getting any error?

 

Proud to be a Super User!!

not error. But not find desired output

MHTANK_0-1742471479343.png

Here Custom=

MHTANK_0-1742471600408.png

 

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])
)

Proud to be a Super User!!

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
)
)

Proud to be a Super User!!

Ya,

It's working very well

Thank you so much👍👍👍

Keep Doing DAX!!

Kuddos are Appreciated

Proud to be a Super User!!

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.