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
FooF0x
Frequent Visitor

Filter table to get the last value per user

Hi,

 

I would like to calculate the score of someone based on its results compared to the results of the other users. I'm currently stuck when I filter the table. Let me explain.

 

I have users and categories. Each user has a result per category and the date when he got his result.

 

I would like to get, for each row, the score which is the result compared to the previous ones.

 

The range of values to take for 1 row (so for 1 manager in 1 category at a certain date) is the last result of each user for this category before the date of the row.

 

Getting all the results for a single category before the date is fairly easy but I can't find a way to only take the last results of each user in this range.

 

VAR PreviousScores= FILTER( 'Results' , 

    'Results'[CatID] = EARLIER( 'Results'[CatID] )

    && 'Results'[Date] <= EARLIER( 'Results'[Date] )

)

 

This gives me the results for the category and before the date of the current row.

 

But within those rows, I only need the last results for each user. This is where I'm stuck.

 

RowUsernameDateResult
2User 110/01/202030
3User 115/10/201920
4User 110/09/201924
5User 210/01/202050
6User 215/10/201945
7User 210/09/201933
8User 310/01/202010
9User 315/10/201922
10User 310/09/201935
11User 44/01/202056
12User 45/10/201934
13User 42/09/201923

 

For example, in this scenario, I have the results for 1 category. To get the scores, here are the values I should use:

  • For row 2: use 2, 5, 8, 11
    • For the first row (10/01/2020), we take the last results of each user on this date. We can see that the dates can be different.
    • The last result of User 4 is on the 4th of Jan so this is the one I use here.
  • Row 3: 3, 6, 9, 12
  • Row 4: 4, 7, 10, 13
  • ...
  • Row 11: 3, 6, 9, 11
    • This result is on the 04/01/2020 so we need the last results of the other user before the 04/01/2020.
  • Row 12: 4, 7, 10, 13

 

What would be the best way to filter these rows in my DAX function for the calculated column?

 

Thank you very much for your help

3 REPLIES 3
amitchandak
Super User
Super User

The information you have provided is not making the problem clear to me. Can you please explain with an example. If possible please share a sample pbix file after removing sensitive information.
Thanks.

My Recent Blog -
Time Intelligence - Direct Query

Hi,

 

Download the file 


Sorry for the delay, here a sample of the data. In the table, I added a calculated column. This column only returns the number of rows to consider. This number should, in this case, always be 4 or 3 for the very first ones.

 

In any case, max 4 as there are only 4 users and I always need the last score for each of them.

 

Thank you very much for your help,

Hi,

I'll try to give more explanations. I'll provide a pbix file as soon as I can.

 

So I have a table with all the results for every user in every category. One user can have multiple results for the same category. He got them at different dates.

 

So my table has the following column: UserID, CategoryID, Date and Result.

 

For every row, I need to compute a score. This score is based on all the latest results of each user previous to this one.

 

So I'm adding a calculated column to do this. In it, I'm filtering this table to only get the results that I need and this is where I'm stuck, I cannot find a way to filter the table to get what I need.

 

So my first step was to get all the results previous to the row.

 

VAR PreviousResults = FILTER( 'Results' ,
'Results'[CategoryID] = EARLIER( 'Results'[CategoryID] )
&& 'Results'[Date] <= EARLIER( 'Results'[Date] )
)

 

But it's not finished quite yet because if User 1 has 3 results for this category before the row, I want to only get the latest because all the older ones are irelevant. This is what I say when I write "All the latest results of each user before the row".

 

I'm stuck at this second step.

 

Considering the following table is the results for 1 category:

UsernameDateResult
User 11/01/202030
User 11/12/201940
User 11/11/201933
User 11/10/201945
User 11/09/201922
User 21/01/202035
User 21/12/201923
User 21/11/201965
User 21/10/201910
User 21/09/201930
User 32/01/202045
User 32/12/201960
User 32/11/201950
User 32/10/201924
User 32/09/201960


In my new column, for the first row which is User 1 on the 01/01/2020, I need the latest scores of all the user on the 01/01/2020. So I'll get User 1 on the 01/01/2020, User 2 on the 01/01/2020 and User 3 on the 02/12/2019 (This is its 1st score past to 01/01/2020).

 

For User 1 on the 01/12/2019, I need User 1 on the 01/12/2019, User 2 on the 01/12/2019 and User 3 on the 02/11/2019.

 

For User 3 on the 02/01/2020, I need User 1 on the 01/01/2020, User 2 on the 01/01/2020 and User 3 on the 02/01/2020.

 

For User 3 on the 02/12/2019, I need User 1 on the 01/12/2019, User 2 on the 01/12/2019 and User 3 on the 02/12/2019.

 

I hope this makes more sense. Let me know if I need to provide more explanations.

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.