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
Anonymous
Not applicable

Create range based on previous row

I have this database with columns A and B and I would like to create columns C and D (START, END), in DAX or Power Query.

Fernandes_20_0-1673010888022.png

 

Column D would just repeat column B, but column C should be ordered and create the range with the previous value of each Item.

 

Is this possible to be created?

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Anonymous 
If you wish to use DAX you may try

Start =
MAXX (
    FILTER (
        CALCULATETABLE (
            VALUES ( 'Table'[VALUE] ),
            ALLEXCEPT ( 'Table', 'Table'[ITEM] )
        ),
        'Table'[Value] < EARLIER ( 'Table'[Value] )
    ),
    'Table'[Value]
)

View solution in original post

2 REPLIES 2
tamerj1
Super User
Super User

Hi @Anonymous 
If you wish to use DAX you may try

Start =
MAXX (
    FILTER (
        CALCULATETABLE (
            VALUES ( 'Table'[VALUE] ),
            ALLEXCEPT ( 'Table', 'Table'[ITEM] )
        ),
        'Table'[Value] < EARLIER ( 'Table'[Value] )
    ),
    'Table'[Value]
)
giammariam
Super User
Super User

@Anonymous, I ended up going with a Power Query implementation. I did it pretty hastily so if you need me to go back and annotate the steps, let me know. Essentially though, if the column names are the same, you would just need to update the source (first step) to use your data. 

 

giammariam_0-1673017086681.png


Here are the steps I took using M:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTK21DE1VYrVgfBMDHTMDBA8Cx1jiJwTkGegY2gA5xjqmCJzzIGcWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Item = _t, Value = _t]),
    #"Sorted Rows" = Table.Sort(Source,{{"Item", Order.Ascending}, {"Value", Order.Ascending}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Sorted Rows",{{"Item", type text}, {"Value", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Item"}, {{"Count", each _, type table [Item=nullable text, Value=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "i", each Table.AddIndexColumn([Count], "Index", 1)),
    #"Expanded Index" = Table.ExpandTableColumn(#"Added Custom", "i", {"Value", "Index"}, {"Value", "Index Grouped"}),
    #"Added Index" = Table.AddIndexColumn(#"Expanded Index", "Index", 0, 1, Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Added Index",{"Count"}),
    #"Added Custom2" = Table.AddColumn(#"Removed Columns", "Lag Item", each try List.Range(Source[Item], [Index]-1, 1) otherwise null),
    #"Expanded Lead Item" = Table.ExpandListColumn(#"Added Custom2", "Lag Item"),
    #"Added Custom3" = Table.AddColumn(#"Expanded Lead Item", "Lag Value", each try List.Range(Source[Value], [Index]-1, 1) otherwise null),
    #"Expanded Lead Value" = Table.ExpandListColumn(#"Added Custom3", "Lag Value"),
    #"Added Custom1" = Table.AddColumn(#"Expanded Lead Value", "START", each if [Index Grouped] = 1 then 0 else if [Item] = [Lag Item] then [Lag Value] else null),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Index Grouped", "Index", "Lag Item", "Lag Value"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns1",{{"START", type text}}),
    #"Added Custom4" = Table.AddColumn(#"Changed Type1", "END", each [Value])
in
    #"Added Custom4"

 

Here is a .pbix that acts as an example

 

If this gets you what you need, please mark this as the solution.



Madison Giammaria
Proud to be a Super User 😄
LinkedIn

Do you frequently use Deneb to provide insights to your stakeholders? Have you considered sponsoring this free and open source custom visual? More info here!

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.

Top Solution Authors