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

Power BI is turning 10! Letโ€™s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
cescovedo
Regular Visitor

CALCULATE SUM for all UserIDs when UserID text is contained in the string value of another column

Hi, I'm trying to CALCULATE the SUM of meeting hours for each userID, by creating a new table from a reference table. However, I have a column for primary userID, and a separate column for secondary userIDs, which is a string of comma separated userIDs. I can do it easily for the primary userID column, but I'm unable for the secondary userIDs, probably because I need to search the value for text by iterating through each userID. I tried transforming the secondary userID column to expand into additional rows and separate the comma-separated string, but then that I would have to calculate the sum of distinct values for the primary userID column, and can't find a way to do that either.

 

'Meeting Hours' table

MeetingIDMeetingHoursPrimaryUserIDSecondaryUserID
111011, 12
20.51110,12,13,14,15
311213
40.251310, 12
50.51410, 13
60.51511,14

 

'Summary' table

PrimaryUserIDPrimaryMeetingHoursSecondaryMeetingHours
1011.25
110.51.5
1211.75
130.252
140.51
150.50.5
2 ACCEPTED SOLUTIONS
OwenAuger
Super User
Super User

Hi @cescovedo 

Here is how I would suggest setting up the model (PBIX attached).

I have added some made-up attributes for illustrative purposes.

OwenAuger_0-1739747863855.png

User (dimension)

OwenAuger_1-1739747920319.png

Meeting (dimension)

OwenAuger_2-1739747934367.png

MeetingHours (fact)

OwenAuger_3-1739747952357.png

MeetingUser (fact/bridge)

OwenAuger_4-1739748000138.png

MeetingUser is constructed by splitting SecondaryUserID by comma into rows, then appending Primary & Secondary users (see Power Query).

 

You can then create measures like:

 

Meeting Hours = 
SUM ( MeetingHours[MeetingHours] )
Primary Meeting Hours = 
CALCULATE (
    [Meeting Hours],
    KEEPFILTERS ( MeetingUser[UserType] = "Primary" )
)
Secondary Meeting Hours = 
CALCULATE (
    [Meeting Hours],
    KEEPFILTERS ( MeetingUser[UserType] = "Secondary" )
)

 

To construct a visual similar to your Summary table, you can either:

1. Create a Matrix with User[UserID] on rows, MeetingUser[UserType] on columns, and Meeting Hours on Values.

2. Create a Table/Matrix with User[UserID] on rows, and two measures Primary Meeting Hours and Secondary Meeting Hours.

OwenAuger_5-1739748294241.png

Would something like this work for you?


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

Hey @cescovedo 

Sure thing, I had another look at this and attached an updated PBIX ๐Ÿ™‚

I adjusted a things. The key changes are:

  • You should generally use User[EISPhusicianId] in visuals, not MeetingUser[EISPhusicianId]. I changed this in the table visual.
    The reason is that User is intended to be the User dimension that filters all other related tables. MeetingUser is a bridge table that ensures Users are related to both their Primary & Secondary meetings, but doesn't need to be visible when creating reports. I have hidden MeetingUser for now.
  • Adjusted the Unscheduled Hours measure as follows:
    Unscheduled Hours = 
    SUMX (
        User,
        [Available Hours] - [MeetingHours]
    )
    SUMX iterates over User and adds Available Hours - MeetingHours for each.
    MeetingHours should always be equal to Primary Meeting Hours + Secondary Meeting Hours (as long as the same user isn't Primary & Secondary in the same meeting).
  • In the visual below, note that Primary & Secondary Hours totals only count each meeting once. You could sum these per User by wrapping in SUMX ( User, ... ) if you wanted.

Hope that's some help!

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

7 REPLIES 7
cescovedo
Regular Visitor

Amazing, thank you! This makes a lot more sense to do it this way. I was able to build the model and tables the way you outlined, but I think I'm going wrong somewhere with the calculation measures, because the hours are adding up to "10" for every UserId. I did get an error that one of my MeetingHours columns was text instead of numeric, so I fixed that, but that didn't fix the issue with the visualization tables/matrices.

 

cescovedo_0-1739822420525.png

 

I don't see a way to attach my pbix file, how did you do that?

Hi @cescovedo 

Thanks for the update!

Actually attaching files is a Super User-only feature on these forums.

 

You can share via Google Drive, OneDrive etc and post the link.

 

In the meantime, one thing to check is the relationship between MeetingUser and Meeting has Cross filter direction set to "Both".

 

regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Hi again,

 

Was hoping you could help me with a related question. In my USER table, I have a new column for how many hours the given user can work. I wanted to create a measure for these, and then create another measure that subtracts the sum of primary+secondary hours from the available hours. It looks like the measure is iterating through every user and not matching on the EISPhysicianID.

 

This is partly because I don't totally understand what you're doing with the Meeting Hours/Primary/Secondary measures. It seems like you're using the SUM aggregation measure, and that is iterating through the MeetingUser table for each ID and EISPhysicianID.

 

So perhaps I need a table similar to MeetingUser that joins the Meeting ID, EISPhysicianID, and AvailableHours?

 

I assume I would also have to update the model, but its not letting me make the connection, presumably because I don't have the right columns between those 2 tables.

 

I suppose the main question is: Can you put Measures from different tables into the same matrix, or no?

 

https://drive.google.com/file/d/1iu9UuGfrC5Z97kcjijTjtLyGV3Xi3smD/view?usp=sharing

 

cescovedo_0-1740183686704.png

 

cescovedo_1-1740183705612.png

cescovedo_2-1740183755589.png

 

Hey @cescovedo 

Sure thing, I had another look at this and attached an updated PBIX ๐Ÿ™‚

I adjusted a things. The key changes are:

  • You should generally use User[EISPhusicianId] in visuals, not MeetingUser[EISPhusicianId]. I changed this in the table visual.
    The reason is that User is intended to be the User dimension that filters all other related tables. MeetingUser is a bridge table that ensures Users are related to both their Primary & Secondary meetings, but doesn't need to be visible when creating reports. I have hidden MeetingUser for now.
  • Adjusted the Unscheduled Hours measure as follows:
    Unscheduled Hours = 
    SUMX (
        User,
        [Available Hours] - [MeetingHours]
    )
    SUMX iterates over User and adds Available Hours - MeetingHours for each.
    MeetingHours should always be equal to Primary Meeting Hours + Secondary Meeting Hours (as long as the same user isn't Primary & Secondary in the same meeting).
  • In the visual below, note that Primary & Secondary Hours totals only count each meeting once. You could sum these per User by wrapping in SUMX ( User, ... ) if you wanted.

Hope that's some help!

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Thank you! Yes that seems to have done it. I came across SUMX but thought it wasn't right for some reason. The primary and secondary hours are counting things up appropriately so somehow that seems to be working without SUMX. But I could change it for consistency and future proofing.  Really appreciate your help with this.

That did it! Thank you so much, can't tell you how helpful this was. This makes me want to go back and rethink other projects as well. I may be back here before long haha. Thanks,

 

 

 

OwenAuger
Super User
Super User

Hi @cescovedo 

Here is how I would suggest setting up the model (PBIX attached).

I have added some made-up attributes for illustrative purposes.

OwenAuger_0-1739747863855.png

User (dimension)

OwenAuger_1-1739747920319.png

Meeting (dimension)

OwenAuger_2-1739747934367.png

MeetingHours (fact)

OwenAuger_3-1739747952357.png

MeetingUser (fact/bridge)

OwenAuger_4-1739748000138.png

MeetingUser is constructed by splitting SecondaryUserID by comma into rows, then appending Primary & Secondary users (see Power Query).

 

You can then create measures like:

 

Meeting Hours = 
SUM ( MeetingHours[MeetingHours] )
Primary Meeting Hours = 
CALCULATE (
    [Meeting Hours],
    KEEPFILTERS ( MeetingUser[UserType] = "Primary" )
)
Secondary Meeting Hours = 
CALCULATE (
    [Meeting Hours],
    KEEPFILTERS ( MeetingUser[UserType] = "Secondary" )
)

 

To construct a visual similar to your Summary table, you can either:

1. Create a Matrix with User[UserID] on rows, MeetingUser[UserType] on columns, and Meeting Hours on Values.

2. Create a Table/Matrix with User[UserID] on rows, and two measures Primary Meeting Hours and Secondary Meeting Hours.

OwenAuger_5-1739748294241.png

Would something like this work for you?


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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