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
HarkaranSingh
New Member

Groupby, sort and then count the number of changes

Hi,

I am new to DAX and need to understand how to count the number of changes that occur after I have used group by and sort.

I have the following table:

ProductNoVoyNoremInTimeChangeDateChange
1S134015-01-2018 23:0615-01-2018 21:000
1S122015-01-2018 23:0615-01-2018 22:001
1S111015-01-2018 23:0615-01-2018 21:001
1S1015-01-2018 23:0615-01-2018 21:000
1S220017-12-2018 01:0017-12-2018 00:000
1S210017-12-2018 01:0017-12-2018 01:001
1S2024-12-2018 01:0024-12-2018 01:000

 

Here, I have created the "change" column using R programming language and would like to know how to do it in DAX.

Logic is: First I have grouped by the three columns: ProductNo, VoyNo and InTime. Then I have sorted "rem" column in descending order and then everytime the datetime in "ChangeDate" column changes, it should input 1 in "change" column, if date remains the same as the previous one then it should show 0. CountDistinct doesn't work as sometimes date is changed and then changed again to the first date and countdistinct counts that only as 1 and not 2. Example below:

 

ProductNoVoyNoremInTimeChangeDateChange
1S134015-01-2018 23:0615-01-2018 21:000
1S122015-01-2018 23:0615-01-2018 22:001
1S111015-01-2018 23:0615-01-2018 21:001
1S1015-01-2018 23:0615-01-2018 21:000

 

After group by and sort, first value in "Change" is 0. Then date in "ChangeDate" column changes from row 1 to row 2 and value of 1 in "Change" column reflects that. Similarly, date changes again from row 2 to 3 and again value 1 of "change column reflect that.

 

Basically, I'm trying to calculatethe number of changes per group (based on the grouping and sorting criteria I described earlier) 

 

If it helps, this is how I did it in R

Table%>% group_by(ProductNo, VoyNo, InTime) %>% arrange(desc(rem)) %>%
mutate(change=if_else(ChangeDate!=lag(ChangeDate), 1, 0))

1 ACCEPTED SOLUTION
Zubair_Muhammad
Community Champion
Community Champion

@HarkaranSingh 

 

Try this calculated column

 

It works with your sample data

 

Column =
VAR PreviousChangeDate =
    MINX (
        TOPN (
            1,
            FILTER (
                Table1,
                [ProductNo] = EARLIER ( [ProductNo] )
                    && [VoyNo] = EARLIER ( [VoyNo] )
                    && [InTime] = EARLIER ( [InTime] )
                    && [rem] > EARLIER ( [rem] )
            ),
            [rem], ASC
        ),
        [ChangeDate]
    )
RETURN
    IF (
        OR ( ISBLANK ( PreviousChangeDate ), [ChangeDate] = PreviousChangeDate ),
        0,
        1
    )

harikar.png 


Regards
Zubair

Please try my custom visuals

View solution in original post

2 REPLIES 2
Zubair_Muhammad
Community Champion
Community Champion

@HarkaranSingh 

 

Try this calculated column

 

It works with your sample data

 

Column =
VAR PreviousChangeDate =
    MINX (
        TOPN (
            1,
            FILTER (
                Table1,
                [ProductNo] = EARLIER ( [ProductNo] )
                    && [VoyNo] = EARLIER ( [VoyNo] )
                    && [InTime] = EARLIER ( [InTime] )
                    && [rem] > EARLIER ( [rem] )
            ),
            [rem], ASC
        ),
        [ChangeDate]
    )
RETURN
    IF (
        OR ( ISBLANK ( PreviousChangeDate ), [ChangeDate] = PreviousChangeDate ),
        0,
        1
    )

harikar.png 


Regards
Zubair

Please try my custom visuals

Thanks for your help 🙂

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.