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
echow
Helper II
Helper II

How to show same period last year data based on group by distinct count data

Basically I need help to create the column (LY) - Last year based on the same week. (Highlighted in Red)

The DAX for TY is

 

TY = Calculate( Distinctcount (Sales[MBR_NO]),

        Groupby(Sales, Sales[Category], Sales[YearWeek]))

 

How do I obtain for the LY column so that it will appear as the table below.

 

echow_0-1611149937738.png

Note: The blank row in between 201947 and 202042 is to show that there should be continuation of the remaining year week.

1 ACCEPTED SOLUTION

Hi @echow,

You can refer to the following steps to achieve your requirements:

1. Create a new table with category and index, then use index fields as the 'sort by column' of type fields.

Category = 
DATATABLE ( "Type", STRING, "Index", INTEGER, { { "TY", 1 }, { "LY", 2 } } )

2. Write a measure with switch function to calculate based on current categories.

Measure = 
VAR currType =
    SELECTEDVALUE ( 'Category'[Type] )
VAR currYW =
    MAX ( Sale[YearWeeK] )
RETURN
    SWITCH (
        currType,
        "TY",
            CALCULATE (
                SUM ( Sale[MBR_NO] ),
                ALLSELECTED ( 'Sale' ),
                VALUES ( Sale[Category] ),
                VALUES ( 'Sale'[YearWeeK] )
            ),
        "LY",
            CALCULATE (
                SUM ( Sale[MBR_NO] ),
                FILTER (
                    ALLSELECTED ( 'Sale' ),
                    'Sale'[YearWeeK]
                        = ( LEFT ( currYW, 4 ) - 1 ) * 100
                            + RIGHT ( currYW, 2 )
                ),
                VALUES ( Sale[Category] )
            )
    )

3. Build matrix visual based on the raw table, new table fields, and use measure on value field.

5.png

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

7 REPLIES 7
echow
Helper II
Helper II

Previously I have attempted the above problem with this code, but it appear as blank on the LY which I suspect it's because I don't have a function like the SUMX function.
 
Member Weekly Count Per Sku LY =
VAR CurrentWeek = SELECTEDVALUE(DATE_TABLE[C_WEEK])
VAR CurrentYear = SELECTEDVALUE(DATE_TABLE[C_YEAR])
Var MaxWeekNumber = CALCULATE(max(DATE_TABLE[C_WEEK]),all(DATE_TABLE[CALENDAR_DATE]))
RETURN

CALCULATE(DISTINCTCOUNT(SALES[MBR_NO]),
GROUPBY(SALES, SALES[CATEGORY], DATE_TABLE[C_YEARWEEK]),
FILTER(all(DATE_TABLE),
DATE_TABLE[C_WEEK] = CurrentWeek && DATE_TABLE[C_YEAR] = CurrentYear - 1
))
 
For the SUMX function it works, can I do something similar for distinct count as well?
Sales LY =
VAR CurrentWeek = SELECTEDVALUE(DATE_TABLE[C_WEEK])
VAR CurrentYear = SELECTEDVALUE(DATE_TABLE[C_YEAR])
Var MaxWeekNumber = CALCULATE(max(DATE_TABLE[C_WEEK]),all(DATE_TABLE[CALENDAR_DATE]))

RETURN
SUMX(
FILTER(all(DATE_TABLE),
DATE_TABLE[C_WEEK] = CurrentWeek && DATE_TABLE[C_YEAR] = CurrentYear - 1),
[Sales])

Hi @echow,
Can please share some dummy data with a similar data structure and expected results? It should help us clarify your scenario and test to coding formula.

How to Get Your Question Answered Quickly 

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

@v-shex-msft , I have just added a dummy dataset and the outcome. 

Hi @echow,

You can refer to the following steps to achieve your requirements:

1. Create a new table with category and index, then use index fields as the 'sort by column' of type fields.

Category = 
DATATABLE ( "Type", STRING, "Index", INTEGER, { { "TY", 1 }, { "LY", 2 } } )

2. Write a measure with switch function to calculate based on current categories.

Measure = 
VAR currType =
    SELECTEDVALUE ( 'Category'[Type] )
VAR currYW =
    MAX ( Sale[YearWeeK] )
RETURN
    SWITCH (
        currType,
        "TY",
            CALCULATE (
                SUM ( Sale[MBR_NO] ),
                ALLSELECTED ( 'Sale' ),
                VALUES ( Sale[Category] ),
                VALUES ( 'Sale'[YearWeeK] )
            ),
        "LY",
            CALCULATE (
                SUM ( Sale[MBR_NO] ),
                FILTER (
                    ALLSELECTED ( 'Sale' ),
                    'Sale'[YearWeeK]
                        = ( LEFT ( currYW, 4 ) - 1 ) * 100
                            + RIGHT ( currYW, 2 )
                ),
                VALUES ( Sale[Category] )
            )
    )

3. Build matrix visual based on the raw table, new table fields, and use measure on value field.

5.png

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Sales Table

CALENDAR_DATEMBR_NOItemCategoryYearWeeK
14/10/20191A1A201942
14/10/20193B3B201942
14/10/20193B3B201942
14/10/20194A4A201942
14/10/20195A5A201942
15/10/20196A6A201942
15/10/20197A7A201942
15/10/20199A9A201942
15/10/201913A13A201942
15/10/201914B14B201942
15/10/201914B14B201942
16/10/201918A18A201942
16/10/201918A18A201942
16/10/201921A21A201942
17/10/201922A22A201942
17/10/201923B23B201942
17/10/201924B24B201942
17/10/201933B33B201942
18/10/201936B36B201942
18/10/201937B37B201942
18/10/201940A40A201942
18/10/201940A40A201942
18/10/201940B40B201942
18/10/201941B41B201942
18/10/201942A42A201942
19/10/201943A43A201942
19/10/201944B44B201942
19/10/201944A44A201942
20/10/201951A51A201942
20/10/201952B52B201942
20/10/201955A55A201942
20/10/201956A56A201942
21/10/201957B57B201943
21/10/201957B57B201943
21/10/201957B57B201943
21/10/201960B60B201943
21/10/201963B63B201943
24/10/201964A64A201943
24/10/201965A65A201943
24/10/201965B65B201943
24/10/201968A68A201943
24/10/201969B69B201943
24/10/201970A70A201943
27/10/201971A71A201943
27/10/201971B71B201943
27/10/201973B73B201943
27/10/201975B75B201943
27/10/201977A77A201943
14/10/20201A1A202042
14/10/20202A2A202042
14/10/20203B3B202042
14/10/20203B3B202042
15/10/20206A6A202042
15/10/20207A7A202042
15/10/20208B8B202042
17/10/202025A25A202042
18/10/202038A38A202042
18/10/202039A39A202042
18/10/202040B40B202042
18/10/202041B41B202042
18/10/202042A42A202042
19/10/202043A43A202043
19/10/202044B44B202043
19/10/202045A45A202043
19/10/202046A46A202043
19/10/202047B47B202043
19/10/202048B48B202043
19/10/202049B49B202043
20/10/202055A55A202043
21/10/202061A61A202043
21/10/202062B62B202043
21/10/202062B62B202043
24/10/202064A64A202043
24/10/202070A70A202043

 

Date Table

CALENDAR_DATEC_YEARWEEK
10/10/2019201941
11/10/2019201941
12/10/2019201941
13/10/2019201941
14/10/2019201942
15/10/2019201942
16/10/2019201942
17/10/2019201942
18/10/2019201942
19/10/2019201942
20/10/2019201942
21/10/2019201943
22/10/2019201943
23/10/2019201943
24/10/2019201943
25/10/2019201943
26/10/2019201943
27/10/2019201943
8/10/2020202041
9/10/2020202041
10/10/2020202041
11/10/2020202041
12/10/2020202042
13/10/2020202042
14/10/2020202042
15/10/2020202042
16/10/2020202042
17/10/2020202042
18/10/2020202042
19/10/2020202043
20/10/2020202043
21/10/2020202043
22/10/2020202043
23/10/2020202043
24/10/2020202043
25/10/2020202043

 

Outcome

 TYTYLYLY
CategoryABAB
2019421711  
20194368  
202042841711
2020437568

 

TY = Calculate( Distinctcount (Sales[MBR_NO]),

        Groupby(Sales, Sales[Category], Sales[YearWeek]))

How do I get LY?

amitchandak
Super User
Super User

@echow , for week intelligence you can rank your year week . You have to use a separate week/date table

 

a new column

Week Rank = RANKX(all('Date'),'Date'[Year Week],,ASC,Dense) //YYYYMM format

 

and try measure like

This Week = CALCULATE(Distinctcount ('sales'[MBR_NO]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])))
Last Week = CALCULATE(Distinctcount ('sales'[MBR_NO]), FILTER(ALL('Date'),'Date'[Week Rank]=max('Date'[Week Rank])-1))
Last year Week= CALCULATE(Distinctcount ('sales'[MBR_NO]), FILTER(ALL('Date'),'Date'[Week Rank]=(max('Date'[Week Rank]) -52)))
Last 8 weeks = CALCULATE(Distinctcount ('sales'[MBR_NO]), FILTER(ALL('Date'),'Date'[Week Rank]>=max('Date'[Week Rank])-8 && 'Date'[Week Rank]<=max('Date'[Week Rank])))

 

Power BI — Week on Week and WTD
https://medium.com/@amitchandak.1978/power-bi-wtd-questions-time-intelligence-4-5-98c30fab69d3
https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...
https://www.youtube.com/watch?v=pnAesWxYgJ8

 

Power BI — Year on Year with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a
https://www.youtube.com/watch?v=km41KfM_0uA

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

My Sales table is actually connected to a calendar table. I have just posted another code above which probably mimic to what you have, but I probably need a DISTINCTCOUNTX function, which does not exist.

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.