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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
Balhemkan
Helper III
Helper III

Sum and average based on selection

  • Hello Team, 

 

I need your help on below issue. Please help me

 

I am using date slicer (start date and end date) in my dashboard

 

my problem is, If i select only one month in slicer it should show sum of sales for that month

but if i select more than one month then it should show average of those months

 

example: i selected Jan5th to Jan 20th in slicer

then it should take from Jan 1st to Jan 31st and should give total of that month.

If i select from Jan 5th to Feb 10th then it should take Min value as Jan 1st and end date will be feb 28th and it should show average of two months.

 

Please help me with dax. 

 

Thanks, 

Balhemkan

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Balhemkan ,

The measure calculates based on the date column. There is no date on the card so that it is blank. You can use KPI visual to show.

vpollymsft_0-1664266517393.png

 

Or use another measure in the card.

Measure2 =
VAR _min =
    MIN ( 'Table'[Date] )
VAR _max =
    MAX ( 'Table'[Date] )
VAR _sales =
    CALCULATE (
        SUM ( volume[Sales] ),
        FILTER ( volume, volume[month] = MONTH ( _min ) )
    )
VAR _diff =
    DATEDIFF ( _min, _max, MONTH ) + 1
RETURN
    _sales / _diff

vpollymsft_1-1664266588243.png

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

11 REPLIES 11
Balhemkan
Helper III
Helper III

Hi, 

I mean if only one month is selected in slicer then sum of that month. If two months selected in slicer sum of 1st +sum of 2nd month average we want. 

Could you please suggest on this

Hi,

I think my sample does not suit your inquiry. Could you please share your sample pbix file's link (onedrive, googledrive, dropbox, any others) here, and also the desired result, and then I can try to look into it to come up with a more accurate solution.

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.

Hi, 

Sorry to bother you. Do you have any update on pbix. I'm completely depending on you. 

 

Hi,

Thank you for your shring the link.

I checked your sample, but I am not sure if it looks like below.

Jihwan_Kim_0-1664259665999.png

 

 

If so, I think the measure shows the correct number.

Or, could you please describe what numbers you want to see as a result?

 

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.

If one month is selected in slicer then it should be 48355.If two months is selected in slicer then it should be 48355/2 =24177.5 like this

Anonymous
Not applicable

Hi @Balhemkan ,

Please refer to my pbix file to see if it helps you.

vpollymsft_0-1664262090602.png

vpollymsft_1-1664262102493.png

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

This is not working on card visual and it showing blank. Could you please suggest me

Anonymous
Not applicable

Hi @Balhemkan ,

The measure calculates based on the date column. There is no date on the card so that it is blank. You can use KPI visual to show.

vpollymsft_0-1664266517393.png

 

Or use another measure in the card.

Measure2 =
VAR _min =
    MIN ( 'Table'[Date] )
VAR _max =
    MAX ( 'Table'[Date] )
VAR _sales =
    CALCULATE (
        SUM ( volume[Sales] ),
        FILTER ( volume, volume[month] = MONTH ( _min ) )
    )
VAR _diff =
    DATEDIFF ( _min, _max, MONTH ) + 1
RETURN
    _sales / _diff

vpollymsft_1-1664266588243.png

Best Regards

Community Support Team _ Polly

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

It really works. 

Thank a lot

Hi @Jihwan_Kim 
Actually your measure is right but for my dataset it is not working. I have attached pbix. Could you please help me dax in that file.Pbix Attachment 

Jihwan_Kim
Super User
Super User

Hi, 

I am not sure if I understood your question correctly, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

I hope the below can provide some ideas on how to create a solution for your datamodel.

 

Jihwan_Kim_1-1664190365299.png

 

 

Jihwan_Kim_0-1664190352168.png

 

Expected result measure: =
VAR _calendartable =
    SUMMARIZE ( 'Calendar', 'Calendar'[Month] )
VAR _monthlyqty =
    ADDCOLUMNS (
        _calendartable,
        "@qty",
            CALCULATE (
                SUM ( Sales[Quantity] ),
                ALLEXCEPT ( 'Calendar', 'Calendar'[Month Name], 'Calendar'[Month] )
            )
    )
RETURN
    SWITCH (
        TRUE (),
        COUNTROWS ( _calendartable ) = 1, SUMX ( _monthlyqty, [@qty] ),
        COUNTROWS ( _calendartable ) > 1, AVERAGEX ( _monthlyqty, [@qty] )
    )

 

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.

Helpful resources

Announcements
Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

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