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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

Reply
clubspec
Helper III
Helper III

Add an average line to line and stacked column chart

Hi Gurus,

I want to add an average Qty line like below picture on a "line and stacked column chart".  The Qty table is linked to a calendar table.  And there is a slicer for 'Calendar' [Year].  How can I write the dax for this average line even when I drill up or down to see average by year or by quarter.

clubspec_0-1728958037690.png

Appreciate you help in advance, I can't get my head around.

Thanks,

clubspec

 

1 ACCEPTED SOLUTION

I got it working today but had to twist a little bit from your DAX to suit my case.  I cannot use AVERAGEX because my data contains multiple rows in same date or same month so if I used AVERAGEX it would be out.  I had to use SUM instead and created extra calculated columns on the fact table for the year, quarter, and month when transaction happended.  But yes, the ISINSCOPE definitely the way to go.

Average sales: =
SWITCH (
    TRUE (),
    ISINSCOPE ( 'Calendar'[Month] ),
    DIVIDE(CALCULATE(SUM(SALESTABLE[Qty (kg)]),ALLEXCEPT('Calendar','Calendar'[Year])),CALCULATE(DISTINCTCOUNTNOBLANK(SALESTABLE[TRANS_MONTH]),ALLEXCEPT('Calendar','Calendar'[Year]))),
    ISINSCOPE ( 'Calendar'[Quarter] ),
    DIVIDE(CALCULATE(SUM(SALESTABLE[Qty (kg)]),ALLEXCEPT('Calendar','Calendar'[Year])),CALCULATE(DISTINCTCOUNTNOBLANK(SALESTABLE[TRANS_QTR]),ALLEXCEPT('Calendar','Calendar'[Year]))),
    ISINSCOPE ('Calendar'[Year]),
    DIVIDE(CALCULATE(SUM(SALESTABLE[Qty (kg)]),ALLSELECTED('Calendar'[Year])),CALCULATE(DISTINCTCOUNTNOBLANK(SALESTABLE[TRANS_YEAR]),ALLSELECTED('Calendar'[Year]))))

 

View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

Please try using ISINSCOPE DAX function in the measure, something like below.

 

The visualization has drill down arrow and you can try to click and drill down to year-quarter or to year-month.

 

Jihwan_Kim_1-1728962364416.png

 

 

Jihwan_Kim_0-1728962334226.png

 

ISINSCOPE function (DAX) - DAX | Microsoft Learn

 

Average sales: = 
SWITCH (
    TRUE (),
    ISINSCOPE ( 'calendar'[Year-MOnth] ),
        AVERAGEX (
            ALL (
                'calendar'[Year-MOnth],
                'calendar'[Year-Month sort],
                'calendar'[Year-Quarter]
            ),
            [Sales total:]
        ),
    ISINSCOPE ( 'calendar'[Year-Quarter] ),
        AVERAGEX (
            ALL ( 'calendar'[Year-Quarter] ),
            [Sales total:]
        ),
    ISINSCOPE ( 'calendar'[Year] ),
        AVERAGEX (
            ALL ( 'calendar'[Year] ),
            [Sales total:]
        )
)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thank you Jihwan_Kim, your dax returns same value as the column value:

clubspec_0-1728964618122.png

Not sure if it is because my Qty table has more rows that got same date and in your example file your sales table has unique date.
I thought it would be something like CALCULATE(SUM(qty),ALL(month)) / DISTINTCOUNT (month) that has data, in this case CALCULATE(SUM(qty),ALL(month)) / 10 months.
But your ISINSCOPE dax definetly will help solving the dynamic of drilling up or down the year, quarter, and month.

Hi,

Thank you for your message, and I think it is because your calendar dimension table has more columns than what I have in my sample.

I think ISINSCOPE is one of DAX functions that you can use in this type of questions.

Thanks.

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

I got it working today but had to twist a little bit from your DAX to suit my case.  I cannot use AVERAGEX because my data contains multiple rows in same date or same month so if I used AVERAGEX it would be out.  I had to use SUM instead and created extra calculated columns on the fact table for the year, quarter, and month when transaction happended.  But yes, the ISINSCOPE definitely the way to go.

Average sales: =
SWITCH (
    TRUE (),
    ISINSCOPE ( 'Calendar'[Month] ),
    DIVIDE(CALCULATE(SUM(SALESTABLE[Qty (kg)]),ALLEXCEPT('Calendar','Calendar'[Year])),CALCULATE(DISTINCTCOUNTNOBLANK(SALESTABLE[TRANS_MONTH]),ALLEXCEPT('Calendar','Calendar'[Year]))),
    ISINSCOPE ( 'Calendar'[Quarter] ),
    DIVIDE(CALCULATE(SUM(SALESTABLE[Qty (kg)]),ALLEXCEPT('Calendar','Calendar'[Year])),CALCULATE(DISTINCTCOUNTNOBLANK(SALESTABLE[TRANS_QTR]),ALLEXCEPT('Calendar','Calendar'[Year]))),
    ISINSCOPE ('Calendar'[Year]),
    DIVIDE(CALCULATE(SUM(SALESTABLE[Qty (kg)]),ALLSELECTED('Calendar'[Year])),CALCULATE(DISTINCTCOUNTNOBLANK(SALESTABLE[TRANS_YEAR]),ALLSELECTED('Calendar'[Year]))))

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.