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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
LanrexTee
Frequent Visitor

Need help on dax measure for the count on the date of maximum total daily count within each month

Hi Team 

Could you pls help me on either of below two tasks? Even if I get Task1 resolved, it's fine. I dont want to use a calculated table.

 

dataTable

productShoptypeShopnamedate_valmonthnumber
BicycleshopAa12024-04-094
BicycleshopAa22024-04-094
BicycleshopAa32024-04-114
BicycleshopAa42024-04-114
BicycleshopAa52024-04-104
BicycleshopAa62024-04-104
BicycleshopAa72024-04-104
BicycleshopAa82024-05-015
BicycleShopBb12024-04-104
BicycleShopBb22024-04-094
BicycleShopBb32024-04-094
BicycleshopBb42024-05-095
BicycleshopBb52024-05-095
MotorcycleshopAa12024-04-094
MotorcycleshopAa22024-04-094
MotorcycleshopAa32024-04-094
MotorcycleshopAa42024-04-114
MotorcycleshopAa52024-04-114
MotorcycleshopAa62024-04-114
MotorcycleshopAa72024-04-114
MotorcycleShopAa82024-05-095
MotorcycleShopBb12024-04-094
MotorcycleShopBb22024-04-114
MotorcycleShopBb32024-04-094
MotorcycleShopBb42024-05-015

 

Pivot Table for the dataset above

LanrexTee_0-1713305981756.png

 

1- Task1: I need a measure to get the count of the shopname on the date that the total daily count (of ShopA+ShopB )  is maximum for each month. 

 

Desired Result:

LanrexTee_1-1713306071895.png

 

2- Task2: In case there're multiple days within a month in which total daily maximum count of shopnames for each product is maximum, select the date of the count to display to be on the day which ShopA has highestcount. 

Desired Result would be:

LanrexTee_2-1713306295716.png

 

Thanks

1 ACCEPTED SOLUTION

Hi, @LanrexTee 

Thank you very much for your reply. After my re-editing of my DAX expression, here's a possible DAX:

Task1 =
VAR _seletedmonth =
    SELECTEDVALUE ( 'Table'[date_val].[Month] )
VAR _seletedquater =
    SELECTEDVALUE ( 'Table'[date_val].[Quarter] )
VAR _seletedyear =
    SELECTEDVALUE ( 'Table'[date_val].[Year] )
VAR _seletedday =
    SELECTEDVALUE ( 'Table'[date_val].[Day] )
VAR _table =
    SUMMARIZE (
        'Table',
        'Table'[date_val],
        'Table'[date_val].[Year],
        'Table'[date_val].[Month],
        'Table'[date_val].[Quarter],
        'Table'[date_val].[Date],
        'Table'[shoptype],
        'Table'[product],
        "Count1",
            VAR _shopA =
                CALCULATE (
                    DISTINCTCOUNT ( 'Table'[shopname] ),
                    FILTER (
                        ALLEXCEPT ( 'Table', 'Table'[product] ),
                        'Table'[date_val] = EARLIER ( 'Table'[date_val] )
                            && 'Table'[shoptype] = "shopA"
                    )
                )
            VAR _shopB =
                CALCULATE (
                    DISTINCTCOUNT ( 'Table'[shopname] ),
                    FILTER (
                        ALLEXCEPT ( 'Table', 'Table'[product] ),
                        'Table'[date_val] = EARLIER ( 'Table'[date_val] )
                            && 'Table'[shoptype] = "shopB"
                    )
                )
            RETURN
                CALCULATE ( _shopA + _shopB )
    )
VAR _maxcount =
    MAXX (
        FILTER (
            _table,
            'Table'[date_val].[Month] = _seletedmonth
                || 'Table'[date_val].[Year] = _seletedyear
                || 'Table'[date_val].[Quarter] = _seletedquater
                || 'Table'[date_val].[Date] = _seletedday
        ),
        [Count1]
    )
VAR _correspondday =
    CALCULATE ( MAX ( 'Table'[date_val] ), FILTER ( _table, [Count1] = _maxcount ) )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[shopname] ),
        FILTER ( 'Table', 'Table'[date_val] = _correspondday )
    )

The results are shown in the figure below:

vjianpengmsft_1-1714123505912.png

vjianpengmsft_2-1714123522495.png

vjianpengmsft_3-1714123539817.png

vjianpengmsft_4-1714123570885.png

I've uploaded the PBIX file I used this time below.

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

5 REPLIES 5
LanrexTee
Frequent Visitor

Many thanks @v-jianpeng-msft 

 

Possibly the sample data I provided wasn't sufficient as I tried the above but didn't work. I'm attaching the closest sample pbix with the data similar to the actual data.

I tried a new measure shown in the snip below, the daily maximum count I got for each product  in each month or quarter  is correct. However, if I sum up the the count displayed under each shoptype for each product in the monthly visual (the lower table), it isnt matching the actual total daily count (upper table), it is instead giving me the maximum in a month for each shoptype.

Note that I need a distinct count of the shopname each day because there'e duplicates and I dont want the shopname to be counted twice for each product in the same day.

All I need is to get the distinctcount of  (shopname) for each product  under each shoptype on the day that the total distinct count(shopname) is maximum in each month. I'll like that to be correct for the Quarter or year as well.

 

Pls see the link to my pbix, kindly help me review the correct fix for this:

 

https://drive.google.com/file/d/14TCLlLFUEIT9ebSACxmmAKkiedUzlR8Z/view?usp=sharing

 

I'm using

Count of shopname max per date_val =
MAXX(
    KEEPFILTERS(VALUES('Table'[date_val])),
    CALCULATE(DISTINCTCOUNT('Table'[shopname]))
)

LanrexTee_1-1713563237359.png

 

Thanks

Lanrex

Hi, @LanrexTee 

Thank you very much for your reply. Do you need a distinct count of shopnames for the day when the sum of shoptype columns, ShopA+ShopB, is the largest sum of shoptypes, or do you need distinct counts of shopnames for each day of the month?

 

Best Regards

Jianpeng Li

Thank you @v-jianpeng-msft , I need it for the single day in each month (and subsequently each Quarter, year depending on the drill down of the date column)  where the count of shopA+ShopB is maximum.

Hi, @LanrexTee 

Thank you very much for your reply. After my re-editing of my DAX expression, here's a possible DAX:

Task1 =
VAR _seletedmonth =
    SELECTEDVALUE ( 'Table'[date_val].[Month] )
VAR _seletedquater =
    SELECTEDVALUE ( 'Table'[date_val].[Quarter] )
VAR _seletedyear =
    SELECTEDVALUE ( 'Table'[date_val].[Year] )
VAR _seletedday =
    SELECTEDVALUE ( 'Table'[date_val].[Day] )
VAR _table =
    SUMMARIZE (
        'Table',
        'Table'[date_val],
        'Table'[date_val].[Year],
        'Table'[date_val].[Month],
        'Table'[date_val].[Quarter],
        'Table'[date_val].[Date],
        'Table'[shoptype],
        'Table'[product],
        "Count1",
            VAR _shopA =
                CALCULATE (
                    DISTINCTCOUNT ( 'Table'[shopname] ),
                    FILTER (
                        ALLEXCEPT ( 'Table', 'Table'[product] ),
                        'Table'[date_val] = EARLIER ( 'Table'[date_val] )
                            && 'Table'[shoptype] = "shopA"
                    )
                )
            VAR _shopB =
                CALCULATE (
                    DISTINCTCOUNT ( 'Table'[shopname] ),
                    FILTER (
                        ALLEXCEPT ( 'Table', 'Table'[product] ),
                        'Table'[date_val] = EARLIER ( 'Table'[date_val] )
                            && 'Table'[shoptype] = "shopB"
                    )
                )
            RETURN
                CALCULATE ( _shopA + _shopB )
    )
VAR _maxcount =
    MAXX (
        FILTER (
            _table,
            'Table'[date_val].[Month] = _seletedmonth
                || 'Table'[date_val].[Year] = _seletedyear
                || 'Table'[date_val].[Quarter] = _seletedquater
                || 'Table'[date_val].[Date] = _seletedday
        ),
        [Count1]
    )
VAR _correspondday =
    CALCULATE ( MAX ( 'Table'[date_val] ), FILTER ( _table, [Count1] = _maxcount ) )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[shopname] ),
        FILTER ( 'Table', 'Table'[date_val] = _correspondday )
    )

The results are shown in the figure below:

vjianpengmsft_1-1714123505912.png

vjianpengmsft_2-1714123522495.png

vjianpengmsft_3-1714123539817.png

vjianpengmsft_4-1714123570885.png

I've uploaded the PBIX file I used this time below.

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

 

 

 

v-jianpeng-msft
Community Support
Community Support

Hi, @LanrexTee 

Based on your description and the data provided, I used the following sample data:

vjianpengmsft_0-1713325420230.png

I use the following DAX expression to create a calculated column to extract the month of the date:

Month = MONTH('Table'[date_val])

vjianpengmsft_1-1713325492363.png

First, for task1, I created a measure using the following DAX expression:

Task1 =
VAR _shopA =
    COUNTAX ( FILTER ( 'Table', 'Table'[Shoptype] = "shopA" ), 'Table'[Shoptype] )
VAR _shopB =
    COUNTAX ( FILTER ( 'Table', 'Table'[Shoptype] = "shopB" ), 'Table'[Shoptype] )
VAR _seletedmonth =
    SELECTEDVALUE ( 'Table'[Month] )
VAR _table =
    SUMMARIZE (
        ALL ( 'Table' ),
        'Table'[date_val],
        "Count1",
            VAR _A =
                CALCULATE (
                    COUNTA ( 'Table'[Shoptype] ),
                    FILTER ( ALLEXCEPT ( 'Table', 'Table'[date_val] ), 'Table'[Shoptype] = "shopA" )
                )
            VAR _B =
                CALCULATE (
                    COUNTA ( 'Table'[Shoptype] ),
                    FILTER ( ALLEXCEPT ( 'Table', 'Table'[date_val] ), 'Table'[Shoptype] = "shopB" )
                )
            RETURN
                _A + _B,
        "Month", MONTH ( 'Table'[date_val] )
    )
VAR _maxcount =
    MAXX ( FILTER ( _table, [Month] = _seletedmonth ), [Count1] )
VAR _correspondday =
    CALCULATE ( MAX ( 'Table'[date_val] ), FILTER ( _table, [Count1] = _maxcount ) )
RETURN
    CALCULATE (
        COUNTA ( 'Table'[Shoptype] ),
        FILTER ( 'Table', 'Table'[date_val] = _correspondday )
    )

Put this measure into the matrix visual as follows:

vjianpengmsft_2-1713325697631.png

For task2, I created a measure using the following DAX expression:

Task2 =
VAR _seletedmonth =
    SELECTEDVALUE ( 'Table'[Month] )
VAR _product =
    SELECTEDVALUE ( 'Table'[product] )
VAR _table =
    SUMMARIZE (
        ALL ( 'Table' ),
        'Table'[date_val],
        'Table'[Shoptype],
        'Table'[Month],
        'Table'[product],
        "Count2", CALCULATE ( COUNTA ( 'Table'[Shoptype] ) )
    )
VAR _maxcount =
    MAXX (
        FILTER ( _table, 'Table'[product] = _product && 'Table'[Month] = _seletedmonth ),
        [Count2]
    )
VAR _correspondday =
    CALCULATE ( MAX ( 'Table'[date_val] ), FILTER ( _table, [Count2] = _maxcount ) )
RETURN
    CALCULATE (
        COUNTA ( 'Table'[Shoptype] ),
        FILTER ( 'Table', 'Table'[date_val] = _correspondday )
    )

Put this measure into the matrix visual as follows:

vjianpengmsft_3-1713325823624.png

I've provided the PBIX file used this time below.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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