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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
decarsul
Helper IV
Helper IV

Rank rows by 'unique' id

Good day all,

 

I'm back with another question, related to my last one.

Working on a set of data, where i want to rank rows based on 2 columns.

 

1st column has a recurring value, which is an identifier of a customer.

2nd column has a unique value.

Each row has a start and end date.

 

Now the first step i want to do is rank the rows to the 1st column.

So if i have 10 rows, with 2 unique customer, where the 1st customer has 3 rows, and the 2nd customer has 7 rows, i want the following output:

 

CustomerIdWorkorderIDRank
111
122
133
2121
2132
2143
2154
2165
2176
2187

 

Now ofcourse, this is logical, but in terms of coding it is not. Because i never defined a rank by criteria. So if this is the result it is pure luck.

Now how would i write this in DAX? In OBIEE i would write something like: RANK(WorkorderID BY CustomerID)

 

I have tried using RankX, but i'm not getting it. So i'm not exactly looking for a answer to my question, i'm more looking for an explanation how to get to that answer.

 

So the last time i asked this question, i got a full answer. But now i'm hoping someone can explain how i get to that answer.

 

This is what i eventually want (per customer id):

 

CustomerIdWorkorderIDRankStart dateEnd dateWithin 7 days of previous workorder? 
1111-1-20191-1-20190False, there is no previous workorder
1222-1-20196-1-20191True, within 1 day a new workorder is created
13311-1-201915-2-20191True, within 7 days of closing last workorder a new one is created

 

For this, i've gotten the following code last time. But its not functioning 100% and since i don't understand the elements i can't fix it. (also this one compares start to start, not end old vs start new)

 

Indicatorx:=VAR thisdate =
MAX ( [Start] )
RETURN
CALCULATE (
COUNT ( [Ticketnr] );
FILTER (
ALLSELECTED ( Table1 );
[UniqueID] = MAX ( [UniqueID])
&& [Start] >= thisdate - 6
&& [Start] <= MAX ( [Start] )
)
)

1 ACCEPTED SOLUTION

Hi, @decarsul 

 

Based on your description, I modified the table as below.

Table:

e1.png

 

You may modified the measure as below. SUM('Table'[WorkorderID]) need to be changed as MAX('Table'[WorkorderID]) because text column can not be used in sum aggregation.

Rank Measure = 
RANKX(
    FILTER(
       ALL('Table'),
       [CustomerId]=MAX('Table'[CustomerId])
    ),
    CALCULATE(MAX('Table'[WorkorderID])),
    ,ASC
)

 

Result:

e2.png

 

Best Regards

Allan

 

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

View solution in original post

8 REPLIES 8
craigbarrien1
New Member

This was great and helped me a lot for my scenario. However, when I try to use this in a bar chart to show how much the price is for each of my categories, the values are showing for the overall total not just the ones whose dynamic ranking is 1. Even when I try to use the measure as a filter it shows blank. I know it's been a few years but any help on this would be great. 

v-alq-msft
Community Support
Community Support

Hi, @decarsul 

 

Based on your description,  I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

e1.png

 

You may create two measures as below..

Rank Measure = 
RANKX(
    FILTER(
       ALL('Table'),
       [CustomerId]=MAX('Table'[CustomerId])
    ),
    CALCULATE(SUM([WorkorderID])),
    ,ASC
)

 

IsWithin 7 Days = 
var r = [Rank Measure]
var lastenddate = 
MAXX(
    FILTER(
        ALL('Table'),
        [CustomerId]=MAX('Table'[CustomerId])&&
        [Rank Measure]=r-1
    ),
    [End Date]
)
var diff = DATEDIFF(lastenddate,MAX('Table'[Start Date]),DAY)
return
IF(
    ISBLANK(lastenddate),
    0,
    IF(
        diff>=7,
        0,1
    )
)

 

First, you need to get the value of 'CustomerId' and use it to filter 'All' table.

FILTER(
       ALL('Table'),
       [CustomerId]=MAX('Table'[CustomerId])
)

Then you may use 'Rankx' function to get the rank by 'WorkorderID' in the context

 of the above filtered table.

RANKX(
    FILTER(
       ALL('Table'),
       [CustomerId]=MAX('Table'[CustomerId])
    ),
    CALCULATE(SUM([WorkorderID])),
    ,ASC
)

 

Result:

e2.png

 

 

Best Regards

Allan

 

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

Hi Allan,

 

The results are exactly what i'm looking for, thanks for that.

For me to get this right. 

 

The Filter in this case acts as my group by statement?

As in, with the filter i select my target group, and by applying that filter in the rankx to ALL(Table) i basically tell it to only do the ranking on each unique customer id that it can find in all columns and rows of the table?

 

Now how does the customerid= max(customerid) define the uniqueness? i don't understand that.

 

Also, What if the unique id is not INT but instead varchar. This won't work with a count 😞

Hi, @decarsul 

 

We limit the scope of the rank by applying that filter in the rankx to ALL(Table). 'customerid= max(customerid)' will filter the rows where customerid equals to the cutomerid in current context in ALL(Table),

 

If the customerid isn't a number but a text. It will have no effect on the result.

 

Best Regards

Allan

 

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

 

Hi Allan,

 

Thank you for getting back to me, and explaining the sorting / filtering. That helps a lot in understanding its basic functionality.

 

I agree that the customer id can be anything, but the workorderid has to be integer right? Because when i change your sample file, remove the integer / number characteristic of the column and add a W infront of the numbers, i nolonger get results.

 

How would i go about that?

decarsul_0-1608102761153.png

 

 

Hi, @decarsul 

 

Based on your description, I modified the table as below.

Table:

e1.png

 

You may modified the measure as below. SUM('Table'[WorkorderID]) need to be changed as MAX('Table'[WorkorderID]) because text column can not be used in sum aggregation.

Rank Measure = 
RANKX(
    FILTER(
       ALL('Table'),
       [CustomerId]=MAX('Table'[CustomerId])
    ),
    CALCULATE(MAX('Table'[WorkorderID])),
    ,ASC
)

 

Result:

e2.png

 

Best Regards

Allan

 

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

Thank you very much for the explenation. i'm using this as the basis of my new report!

I have however added an addition to the if statement in the end, that also if the start date is empty to also show 0.

decarsul
Helper IV
Helper IV

As i don't sit still myself.

I have found the following post: https://community.powerbi.com/t5/Desktop/create-rank-column-based-on-column-using-dax/td-p/567456

 

This code does roughly what i want. But same as above, i don't understand why what is where?

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

Check out the August 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors