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

How to create an index using dax comparing numbers on two columns

Hi everyone,

 

I have a problem to generate the calculated column "INDICE4" I want generate the "indece4" but only change if column "index" is distinct to the previous value. This is the example:

 

indice 4.png

 

The data is:

CityAreaOrganizationItemFromToOperatorIndiceIndexIndice2
N.Y.NY_B2Company_T1Quantity37AND1011
N.Y.NY_B2Company_T1ProductPRD03PRD06OR1122
N.Y.NY_B2Company_T1ProductPRD08PRD013OR1223
N.Y.NY_B2Company_T1ProductPRD15PRD18OR1324
N.Y.NY_B2Company_B5Quantity13AND1415
N.Y.NY_B2Company_B5ProductPRD04PRD06OR1526
N.Y.NY_B2Company_B5ProductPRD09PRD11OR1627
N.Y.NY_D4Company_F5Quantity24AND2211
N.Y.NY_D4Company_F5ProductPRD02PRD05OR2322
N.Y.NY_D4Company_F5ProductPRD07PRD09OR2423
N.Y.NY_D4Company_B2Quantity13AND2514

 

Thank's a lot for help me, best regards...

 

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

In general, it's easier to create index columns in the query editor rather than in DAX, so I'm providing a solution in M instead. It's possible in DAX but I'm passing on writing RANKX stuff for the moment. Maybe another user will jump in.

To get this index, I would group on the first four columns to set the index granularity, then group on City and Area, generating an index for each of these, then expand and merge back with the original table.

Here's the full M query you can paste into your Advanced Editor to walk through the applied steps:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZFBD0NAEIX/i7OInd1Fj1XpUVvpRURE2otDaRoO/n1nGWIJ4jD71ka+zHsvSYzQii3DNMI48wH1Un2+edlmT4YfjyYv66Ju8cpxXJxzGODJbHV0k5rbjPuvejevWt2iwOakDuotUgD1D3RzjOSRMj6igFD8GIpJUm8kcSKJLZIv9YgYxUQRCXqSuwzdmFhEJGkb5yDpRL7YSHKI5M5IgZiQrjNffRCDL4CV6pcMfRsglcM2MKQ8r36P5E789SSx0rxG6uJa7QskPWHn6R8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [City = _t, Area = _t, Organization = _t, Item = _t, From = _t, To = _t, Operator = _t, Indice = _t, Index = _t, Indice2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Area", type text}, {"Organization", type text}, {"Item", type text}, {"From", type text}, {"To", type text}, {"Operator", type text}, {"Indice", Int64.Type}, {"Index", Int64.Type}, {"Indice2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"City", "Area", "Organization", "Item"},{}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"City", "Area"}, {{"AllRows", each  Table.AddIndexColumn(_, "Index4", 1, 1, Int64.Type), type table [City=nullable text, Area=nullable text, Organization=nullable text, Item=nullable text, Index4=Int64.Type]}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows1", "AllRows", {"Organization", "Item", "Index4"}, {"Organization", "Item", "Index4"}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"City", "Area", "Organization", "Item"}, #"Expanded AllRows", {"City", "Area", "Organization", "Item"}, "Expanded AllRows", JoinKind.LeftOuter),
    #"Expanded Expanded AllRows" = Table.ExpandTableColumn(#"Merged Queries", "Expanded AllRows", {"Index4"}, {"Index4"})
in
    #"Expanded Expanded AllRows"

View solution in original post

4 REPLIES 4
AlexisOlson
Super User
Super User

In general, it's easier to create index columns in the query editor rather than in DAX, so I'm providing a solution in M instead. It's possible in DAX but I'm passing on writing RANKX stuff for the moment. Maybe another user will jump in.

To get this index, I would group on the first four columns to set the index granularity, then group on City and Area, generating an index for each of these, then expand and merge back with the original table.

Here's the full M query you can paste into your Advanced Editor to walk through the applied steps:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZFBD0NAEIX/i7OInd1Fj1XpUVvpRURE2otDaRoO/n1nGWIJ4jD71ka+zHsvSYzQii3DNMI48wH1Un2+edlmT4YfjyYv66Ju8cpxXJxzGODJbHV0k5rbjPuvejevWt2iwOakDuotUgD1D3RzjOSRMj6igFD8GIpJUm8kcSKJLZIv9YgYxUQRCXqSuwzdmFhEJGkb5yDpRL7YSHKI5M5IgZiQrjNffRCDL4CV6pcMfRsglcM2MKQ8r36P5E789SSx0rxG6uJa7QskPWHn6R8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [City = _t, Area = _t, Organization = _t, Item = _t, From = _t, To = _t, Operator = _t, Indice = _t, Index = _t, Indice2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Area", type text}, {"Organization", type text}, {"Item", type text}, {"From", type text}, {"To", type text}, {"Operator", type text}, {"Indice", Int64.Type}, {"Index", Int64.Type}, {"Indice2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"City", "Area", "Organization", "Item"},{}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"City", "Area"}, {{"AllRows", each  Table.AddIndexColumn(_, "Index4", 1, 1, Int64.Type), type table [City=nullable text, Area=nullable text, Organization=nullable text, Item=nullable text, Index4=Int64.Type]}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows1", "AllRows", {"Organization", "Item", "Index4"}, {"Organization", "Item", "Index4"}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"City", "Area", "Organization", "Item"}, #"Expanded AllRows", {"City", "Area", "Organization", "Item"}, "Expanded AllRows", JoinKind.LeftOuter),
    #"Expanded Expanded AllRows" = Table.ExpandTableColumn(#"Merged Queries", "Expanded AllRows", {"Index4"}, {"Index4"})
in
    #"Expanded Expanded AllRows"

@AlexisOlson  I try with your M code, and was very difficult because I am new on Power BI, DAX and M.

but, your explanation was very useful.

 

I changed your M code to this:

 

let
Origen = Excel.Workbook(File.Contents("C:\Users\danie\Desktop\Criterio Matriz.xlsx"), null, true),
Criteria_Table = Origen{[Item="Criteria",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Area", type text}, {"Organization", type text}, {"Item", type text}, {"From", type text}, {"To", type text}, {"Operator", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"City", "Area", "Organization", "Item"},{}),
#"Grouped Rows1" = Table.Group(#"Grouped Rows", {"City", "Area"}, {{"AllRows", each Table.AddIndexColumn(_, "Index4", 1, 1, Int64.Type), type table [City=nullable text, Area=nullable text, Organization=nullable text, Item=nullable text, Index4=Int64.Type]}}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows1", "AllRows", {"Organization", "Item", "Index4"}, {"Organization", "Item", "Index4"}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"City", "Area", "Organization", "Item"}, #"Expanded AllRows", {"City", "Area", "Organization", "Item"}, "Expanded AllRows", JoinKind.LeftOuter),
#"Expanded Expanded AllRows" = Table.ExpandTableColumn(#"Merged Queries", "Expanded AllRows", {"Index4"}, {"Index4"})
in
#"Expanded Expanded AllRows"

 

 

The 3rd line  (#"Changed Type" =  ....) give me an error but I must delete that step and works perfect.

 

I really apreciate your explanation, example, code, time, help, etc.

 

Best regards and thank's

 

It gives an error since it's referring to a previous step "Source". When you deleted that step, it automatically re-linked the reference to #"Changed Type" in the #"Grouped Row" step to reference Critera_Table instead, making everything work. 🙂

 

Notice how each line references the prior one as the first argument in the table function and you'll have a better understanding of how to fix reference errors.

Thanks again for the explanation 👍🏼

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.