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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
mbudiman
Helper I
Helper I

Derive Avg based on Dimension from other table

Hello,

 

I need help to derive average shipped qty for last 3 Qtr for each REGION.

 

The sample pbix can be downloaded here Inventory2.pbix

 

I use custom Calendar that contains these columns :

- Fiscal Year, example : FY25, FY24 

- Fiscal Qtr, example : FY25Q1, FY25Q2, FY25Q3

- Qtr_Seq_No, where 0 means current Qtr, -1 means last 1 Qtr, -2 means last 2 Qtr

So to derive last 3 Qtr, we use Qtr_Seq_No -3, -2, -1.

 

Inventory table contains these columns :

- Fiscal Qtr

- Hub

- Product

- Shipped Qty

 

Inventory Region table contains these columns:

- Region

- Hub

 

Table Relationship

Calendar table is linked to Inventory table by Fiscal Qtr column

Inventory Region table in linked to Inventory table by Inventory Hub.

 

The following is the required report format and expected result.

inventory.jpg

 

What is the DAX statement to achieve this ? Thank you in advance for your help.

 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @mbudiman 

 

For the calcuations to be dynamic, the sequence must not be hardcoded but rather must be determined from the currently selected sequence or if the current  one, zero.

Average L3Q = 
VAR CurrentSequence =
    -- The current quarter sequence number in the context.
    MIN ( 'Inventory calendar'[QTR_SEQ_NO] )
VAR OneQtrAgo =
    -- Calculate the sequence number for one quarter ago.
    CurrentSequence - 1
VAR ThreeQtrAgo =
    -- Calculate the sequence number for three quarters ago.
    CurrentSequence - 3
VAR _Numerator =
    -- Calculate the total "Shipped Qty" for the last three quarters.
    CALCULATE (
        SUM ( Inventory[Shipped Qty] ), -- Total shipped quantity
        FILTER (
            ALL ( 'Inventory calendar' ), -- Ignore existing filters on the calendar table
            'Inventory calendar'[QTR_SEQ_NO] >= ThreeQtrAgo
                && 'Inventory calendar'[QTR_SEQ_NO] <= OneQtrAgo -- Restrict to the desired range of quarters
        )
    )
VAR _Denominator =
    -- Calculate the number of quarters in the range dynamically (if constant, use 3 instead).
    VAR ConstantDenominator = 3
    VAR DynamicDenominator =
        CALCULATE (
            COUNTROWS ( 'Inventory calendar' ), -- Count the number of rows (quarters) in the calendar
            FILTER (
                ALL ( 'Inventory calendar' ), -- Ignore existing filters on the calendar table
                'Inventory calendar'[QTR_SEQ_NO] >= ThreeQtrAgo
                    && 'Inventory calendar'[QTR_SEQ_NO] <= OneQtrAgo -- Restrict to the desired range of quarters
            )
        )
    RETURN
        DynamicDenominator
RETURN
    -- Divide the total shipped quantity by the number of quarters to get the average.
    DIVIDE ( _Numerator, _Denominator )

danextian_0-1736604531781.png

 

The last 3 quarters average for each region can be calculated as below:

Average L3Q  - America = 
CALCULATE ( [Average L3Q], 'Inventory Region'[Region] = "America" )

Please see the attached pbix.

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

2 REPLIES 2
danextian
Super User
Super User

Hi @mbudiman 

 

For the calcuations to be dynamic, the sequence must not be hardcoded but rather must be determined from the currently selected sequence or if the current  one, zero.

Average L3Q = 
VAR CurrentSequence =
    -- The current quarter sequence number in the context.
    MIN ( 'Inventory calendar'[QTR_SEQ_NO] )
VAR OneQtrAgo =
    -- Calculate the sequence number for one quarter ago.
    CurrentSequence - 1
VAR ThreeQtrAgo =
    -- Calculate the sequence number for three quarters ago.
    CurrentSequence - 3
VAR _Numerator =
    -- Calculate the total "Shipped Qty" for the last three quarters.
    CALCULATE (
        SUM ( Inventory[Shipped Qty] ), -- Total shipped quantity
        FILTER (
            ALL ( 'Inventory calendar' ), -- Ignore existing filters on the calendar table
            'Inventory calendar'[QTR_SEQ_NO] >= ThreeQtrAgo
                && 'Inventory calendar'[QTR_SEQ_NO] <= OneQtrAgo -- Restrict to the desired range of quarters
        )
    )
VAR _Denominator =
    -- Calculate the number of quarters in the range dynamically (if constant, use 3 instead).
    VAR ConstantDenominator = 3
    VAR DynamicDenominator =
        CALCULATE (
            COUNTROWS ( 'Inventory calendar' ), -- Count the number of rows (quarters) in the calendar
            FILTER (
                ALL ( 'Inventory calendar' ), -- Ignore existing filters on the calendar table
                'Inventory calendar'[QTR_SEQ_NO] >= ThreeQtrAgo
                    && 'Inventory calendar'[QTR_SEQ_NO] <= OneQtrAgo -- Restrict to the desired range of quarters
            )
        )
    RETURN
        DynamicDenominator
RETURN
    -- Divide the total shipped quantity by the number of quarters to get the average.
    DIVIDE ( _Numerator, _Denominator )

danextian_0-1736604531781.png

 

The last 3 quarters average for each region can be calculated as below:

Average L3Q  - America = 
CALCULATE ( [Average L3Q], 'Inventory Region'[Region] = "America" )

Please see the attached pbix.

 










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

hi @danextian,

 

Thank you for the solution with clear explanation. Appreciate that.

 

best regards,

mbudiman

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.