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
IvanMislav
Helper I
Helper I

DAX custom score challenge

 

Hi,

 

Here is the situation. I have two tables: "Messages" and "Users" which are related throught User_id.

 

Mesagges: Message_id, Replied_to_id, User_id

Users: User_id, User_name, Department

 

What i want to do is to create a measure that will calculate some custom Score

Score = If the user replied to a post made by a user from a different department, it counts as 2. If the user replied to a post made by a user from the same department, it counts as 1. If the post does not have Replied_to_id (Initial post), it counts as 0.

 

Does anyone have any suggestions how to do this?

Thanks!

1 ACCEPTED SOLUTION
technolog
Super User
Super User

To solve this challenge, you'll need to use DAX to compare the department of the original poster to the department of the user who replied. You can do this with the RELATED and RELATEDTABLE functions.

Here's a step-by-step solution.

First, create a relationship between the Messages and Users tables on the User_id field, if you haven't done so already.

Create a calculated column in the Messages table to fetch the department of the user who posted the message:

MessageUserDept = RELATED(Users[Department])
Create another calculated column in the Messages table to fetch the department of the user who the message is a reply to:
RepliedToUserDept =
IF(
ISBLANK(Messages[Replied_to_id]),
BLANK(),
RELATED(Users[Department], LOOKUPVALUE(Messages[User_id], Messages[Message_id], Messages[Replied_to_id]))
)
Now, create the Score measure based on your conditions:
Score =
SUMX(
Messages,
SWITCH(
TRUE(),
ISBLANK(Messages[Replied_to_id]), 0,
Messages[MessageUserDept] = Messages[RepliedToUserDept], 1,
Messages[MessageUserDept] <> Messages[RepliedToUserDept], 2,
BLANK()
)
)
This measure iterates over each row of the Messages table and calculates the score based on your conditions.

By using this measure in a visual, you can calculate the score based on any context you set (e.g., filter by specific user, department, or date ranges).

Remember to adjust table and column names if they're different from the ones you provided.

View solution in original post

1 REPLY 1
technolog
Super User
Super User

To solve this challenge, you'll need to use DAX to compare the department of the original poster to the department of the user who replied. You can do this with the RELATED and RELATEDTABLE functions.

Here's a step-by-step solution.

First, create a relationship between the Messages and Users tables on the User_id field, if you haven't done so already.

Create a calculated column in the Messages table to fetch the department of the user who posted the message:

MessageUserDept = RELATED(Users[Department])
Create another calculated column in the Messages table to fetch the department of the user who the message is a reply to:
RepliedToUserDept =
IF(
ISBLANK(Messages[Replied_to_id]),
BLANK(),
RELATED(Users[Department], LOOKUPVALUE(Messages[User_id], Messages[Message_id], Messages[Replied_to_id]))
)
Now, create the Score measure based on your conditions:
Score =
SUMX(
Messages,
SWITCH(
TRUE(),
ISBLANK(Messages[Replied_to_id]), 0,
Messages[MessageUserDept] = Messages[RepliedToUserDept], 1,
Messages[MessageUserDept] <> Messages[RepliedToUserDept], 2,
BLANK()
)
)
This measure iterates over each row of the Messages table and calculates the score based on your conditions.

By using this measure in a visual, you can calculate the score based on any context you set (e.g., filter by specific user, department, or date ranges).

Remember to adjust table and column names if they're different from the ones you provided.

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