Create group index column by using DAX

Create group index column by using DAX

One way to create a group index by using DAX is with the function RANKX. How to do that depends on your dataset structure, but this example might give you a good idea or precise solution.

In another DataCornering post, there was an example with group index by using Power Query or R programming language in Power BI Power Query.

If you are not using the R programming language, then here are some guidelines on how to start learning that.

Group index by using DAX

In my dataset, I have 3 columns – Date, Attribute, and Value. You can recreate that by using the Power Query code below.

DAX group index data set

In the Attribute column are 5 categories, and I will use that to create a group index. For each of the categories in the column Attribute, there are unique date values. The most important here are adjustments of the last two RANKX parameters – Order and Ties.

DateGroupIndex = RANKX(MyTable, MyTable[Date], , ASC, Dense)

group index by using DAX

Indexing restarts every time Attribute value changes.

Global table index by using DAX

To calculate an index for each of the table rows, there might be a situation when none of the columns have unique values and determine the sequence. But if multiple columns can do that in combination, it is possible to use them in RANKX.

In my case, there is a date and the column Attribute with 5 possible categories. A combination of both of them will make every row unique, but it is not straightforward. Some of the combinations are repeating. In RANKX there is possible to use an expression for the ranking. That allows using of nested RANKX to get the results for multiple columns.

If I create indexing for the group Attribute, I get numbers from one to five.

AttributeGroupIndex = RANKX(MyTable, MyTable[Attribute], , ASC, Dense)

another group index by using DAX

I can use that as a column in further calculations or create nested RANKX like this.

GrandIndex =
RANKX (
    MyTable,
    MyTable[DateGroupIndex]
        + RANKX ( MyTable, MyTable[Attribute],, ASC, DENSE )
            * MAX ( MyTable[DateGroupIndex] ),
    ,
    ASC,
    DENSE
)

Conclusion

If you are using the possible granularity of a group and describe combinations mathematically, you can make all kinds of indices.
Maybe it makes sense to do that in Power Query or by using programming language R. In that case, take a look at this post.

If you have DAX performance issues, then look at this post for a more advanced approach.

Here is Power Query code that you can use in a blank query to recreate my dataset.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZtdrt02DIT3kuegEPWvtQR5CNAldP/o8ZA8dswhUOCi+CCfsUSNSFr59etHkX8+/9VS5cfPH//++e/P9VfOlh+/fxJaP3+HJLB9/s69OOyfv2t0DsfnbxtylFYqqQqnl6TeF4eX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N91cnELTSiCMwKJJqInXxx0QYk67cApNoyZP1g/zm0NNnu6rGsQIaudUz18vsZiJy3zcH4lGMJKfVSO472oQTaVzqruuHU7VnGry5K52yiGcwNqlwkzcj8kANZxOMtT6codTDXHhEEu37lKfnb+VU2hafr+BeXjrjz5BPOtO41D74KaJefgnOUqoalrCqV6PrQmFYZbkwVr/3gV71OSfjwNVTXtyim23vV3FTHxJ4VD7KZbjMxP/5nOB2mGXjFXLlMIp4slbAczF5+PaBYmntTmtfymmqfjwr8TMxr+vw2y8mYlUauOlH06tPCicQtOayZNRBGc/Cxuvd0lPNO3GqdYHPRmrl5nq4hQxvoRDfIGWu2gnMS6LU8sKEgpNZU1O4QUiHKIX5rcZqI3LoxtAPNOLRpqLf2Oc2fgqwiE0eZeB5uLdGyPUx+vj6gaJcRmc6hXZ5MF6N87Wjvr497oT9XHxfUd9vJbJqZ4th0P96nt3A8ja1ckp+mE+TTQX/953Yjb+vedDbdyvojEbPz7BzMXnahyqEfTkuV0PAA41T7FrFNTEyxFONZh6QnGlqZ3K6fNfXgSowXTf3iAHcDucWlN8c4pg8q/5gT4PlgCh6TLM3/8D", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Attribute = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Attribute", type text}, {"Value", Int64.Type}})
in
    #"Changed Type"

 





Posted

in

,

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *