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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Anonymous
Not applicable

Generate groups using previous rows

Hi there! I have the following table order by Date DESC:

 

ProductTypeCycleDate
Product 1WONO15/03/2015
Product 1WOYES22/10/2015
Product 1WONO31/03/2016
Product 1WOYES07/08/2017
Product 1WOYES31/09/2017
Product 1WOYES09/10/2017
Product 1WOYES31/11/2017

 

I'm trying to create a new column with the following logic: If the previous row has "Cycle" value of "Yes" then the actual row will have the previous group + 1. The table has different products. Continuing with the example, I will have:  

 

ProductTypeCycleDateGroup
Product 1WONO15/03/20151
Product 1WOYES22/10/20151
Product 1WONO31/03/20162
Product 1WOYES07/08/20172
Product 1WOYES31/09/20173
Product 1WONO09/10/20174
Product 1WOYES31/11/20174

 

Thanks in advance!

Fernando

2 ACCEPTED SOLUTIONS
v-deddai1-msft
Community Support
Community Support

Hi @Anonymous ,

 

You can create an index column in query editor:

Capture3.PNG

 

And the use the following calculated column:

 

 

Column = VAR A = CALCULATE(DISTINCTCOUNT('Table'[Index]),FILTER('Table','Table'[Index]<= EARLIER('Table'[Index])&&'Table'[Cycle] = "YES")) RETURN IF('Table'[Cycle]= "NO",A+1,A)

 

 

For more details, please refer to the pbix file https://qiuyunus-my.sharepoint.com/:u:/g/personal/pbipro_qiuyunus_onmicrosoft_com/EcJOk6WxpPJGgtsQubDfKaUBvgNo2B1qbezD1agnJv8lvw?e=3F4fbx

 

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

 

Best Regards,

Dedmon Dai

 

View solution in original post

Anonymous
Not applicable

Hi everyone!

 

Thanks all for your help. As @lbendlin , @Greg_Deckler  and @v-deddai1-msft  said I needed an index into dataset.

 

I got a solution doing this (i think is very similar to the @v-deddai1-msft solution):

 

Group = 
VAR PreviousRow =
    TOPN (
        1,
        FILTER (
            Table,
            Table[Date] < EARLIER ( Table[Date] )
                && Table[Product] = EARLIER (Table[Product] )
        ),
        [Date], DESC
    )
VAR PreviousCycle =
    MINX ( PreviousRow, [Cycle] )
VAR PreviousIndex =
    MINX ( PreviousRow, [Index] )
RETURN
    If(isblank(PreviousCycle),Table[Index], If (PreviousCycle = "YES", PreviousIndex+1,PreviousIndex))

 

Thanks everyone again!

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi everyone!

 

Thanks all for your help. As @lbendlin , @Greg_Deckler  and @v-deddai1-msft  said I needed an index into dataset.

 

I got a solution doing this (i think is very similar to the @v-deddai1-msft solution):

 

Group = 
VAR PreviousRow =
    TOPN (
        1,
        FILTER (
            Table,
            Table[Date] < EARLIER ( Table[Date] )
                && Table[Product] = EARLIER (Table[Product] )
        ),
        [Date], DESC
    )
VAR PreviousCycle =
    MINX ( PreviousRow, [Cycle] )
VAR PreviousIndex =
    MINX ( PreviousRow, [Index] )
RETURN
    If(isblank(PreviousCycle),Table[Index], If (PreviousCycle = "YES", PreviousIndex+1,PreviousIndex))

 

Thanks everyone again!

v-deddai1-msft
Community Support
Community Support

Hi @Anonymous ,

 

You can create an index column in query editor:

Capture3.PNG

 

And the use the following calculated column:

 

 

Column = VAR A = CALCULATE(DISTINCTCOUNT('Table'[Index]),FILTER('Table','Table'[Index]<= EARLIER('Table'[Index])&&'Table'[Cycle] = "YES")) RETURN IF('Table'[Cycle]= "NO",A+1,A)

 

 

For more details, please refer to the pbix file https://qiuyunus-my.sharepoint.com/:u:/g/personal/pbipro_qiuyunus_onmicrosoft_com/EcJOk6WxpPJGgtsQubDfKaUBvgNo2B1qbezD1agnJv8lvw?e=3F4fbx

 

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

 

Best Regards,

Dedmon Dai

 

FrankAT
Community Champion
Community Champion

Hi @Anonymous 

are you looking for the following solution?

 

07-09-_2020_23-38-55.png

Measure = 
VAR _ActualDate =
    MIN ( 'Table'[Date] )
VAR _PreviousDate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Date] < _ActualDate )
    )
VAR _Group =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Product] ),
            'Table'[Cycle] = "Yes"
                && 'Table'[Date] <= MIN ( 'Table'[Date] )
        )
    )
RETURN
    _Group

 

With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
FrankAT (Proud to be a Datanaut)

Greg_Deckler
Community Champion
Community Champion

@Anonymous - Do you have or can you add an Index? Otherwise, is "previous row" defined by the date column? If there are no previous rows is the group 1?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
lbendlin
Super User
Super User

I think you meant ASCending sort.  Can you add an index column to your source data? If yes then you can use that when you add the group column.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKTylNLlEwVNJRCvcHEn4gwtBU38BY38jA0FQpVgeLokjXYCBpZKRvaIBHFdgoY0NijDIw1zewAKkyx6cKZJYlHlVgC4EqIK4iZJShIVRVLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Product = _t, Type = _t, Cycle = _t, Date = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Group", 
each 1+List.Count(
    List.Select(
        Table.Column(
            Table.FirstN(#"Added Index",[Index])
            ,"Cycle")
        ,each _ ="YES")
    )
)
in
    #"Added Custom"

 

 

Note that your sample data is inconsistent.

 

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors