Forum Discussion

MHTANK's avatar
MHTANK
Icon for Helper III rankHelper III
1 year ago
Solved

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.

  • 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

  • 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!

    • MHTANK's avatar
      MHTANK
      Icon for Helper III rankHelper III

      Ya, this is good,
      But I want this in the report matrix.

      • Khushidesai0109's avatar
        Khushidesai0109
        Icon for Skilled Sharer rankSkilled 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



        MHTANK