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

Summarize Table and Sum duplicates ids

I have two tables "Donor" and "Giving" (see below) joined by [user_id] that are used to generate a 3rd table for export.  

 

The Requirements:
1) Calculate total [amount] given by [family_id]

2) Grab both [first_name] fields when 2 donors share the same [family_id]
    * Note below where [family_id] = 207 because of a child;  [marital_status] = "Married" identifies which names to pull

Sample Output: (from tables below)

family_id      total       donor1    donor2
200250Barrett 
204100RogerClementine
207425FreddyMaggie
208135Piper 

 

 

Thanks for the help! Please see below for sample tables.

 

Bryan

 

****************************************************************

 

 

Donor Table:

user_id  family_id  first_name       last_name   Marital Status
1200BarrettAureliaSingle
2201RhiannonBaileyDivorced
3202QuynAureliaSingle
4203HermanCastilloMarried
4204RogerHoganMarried
5204ClementineHoganMarried
6205MendezAugustDivorced
7206KeefeBlakeSingle
8207FreddyCruzMarried
9207MaggieCruzMarried
10207JohnnyCruz 
11208PiperSerranoSingle



Giving Table:

id         user_id    date       amount 
700065-Jan25
700185-Jan60
700235-Jan116
700345-Jan40
7004108-Feb30
700588-Feb200
700678-Feb21
7007108-Feb70
700898-Feb40
700923-Mar36
701013-Mar200
701163-Mar50
701243-Mar60
7013103-Mar35
7014816-Apr100
7015116-Apr50
7016916-Apr25
7017216-Apr39
7018316-Apr120

 

1 ACCEPTED SOLUTION

I'm not typing all of my stuff again. in summary, i changed roger to 12, but there is no donor data in 12 so nothign shows up.

Donors =
VAR varCurrentFamily =
    SUMMARIZE(
        'Donor Table',
        'Donor Table'[family_id  ],
        'Donor Table'[user_id  ],
        'Donor Table'[Marital Status]
    )
VAR varMemberCount =
    COUNTROWS( varCurrentFamily )
VAR varValidDonors =
    IF(
        varMemberCount > 2,
        CONCATENATEX(
            CALCULATETABLE(
                'Donor Table',
                'Donor Table'[Marital Status] = "Married"
            ),
            [first_name       ],
            ", ",
            [first_name       ], ASC
        ),
        CONCATENATEX(
            'Donor Table',
            'Donor Table'[first_name       ],
            ", ",
            'Donor Table'[first_name       ], ASC
        )
    )
RETURN
    IF(
        ISINSCOPE( 'Donor Table'[family_id  ] )
            && [Total Giving]
                <> BLANK(),
        varValidDonors,
        BLANK()
    )

This measure will look for families with more than 2. If it finds that, it only pulls married, otherwise it does the normal stuff. the PBIX file I linked to above can be used again. It has this new code. There is probably a more efficient way to do this, but this works.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

8 REPLIES 8
edhans
Super User
Super User

Will this work @bmurf 

edhans_1-1619562369038.png

 

Rather than putting the two donors in seperate columns, I used CONCATENATEX to create a list of up to two people. It doesn't match your output exactly because our DONOR table had a duplicate #4 record, so I whacked it because something has to be unique, and I assumed that was the user_ID.

 

Donors = 
VAR varValidDonors = 
    CALCULATETABLE(
        'Donor Table',
        'Donor Table'[Marital Status] <> BLANK()
    )
VAR varDonorList = 
   CONCATENATEX(
        varValidDonors,
        'Donor Table'[first_name       ], 
        ", ",
        'Donor Table'[first_name       ], 
        ASC
    )
RETURN
    IF(
        ISINSCOPE('Donor Table'[family_id  ])
            && [Total Giving] <> BLANK(),
        varDonorList,
        BLANK()
    )

 My PBIX file is here if you want to see the whole thing. 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
bmurf
Frequent Visitor

edhans,

I worked thru it - concatnatex is cool, never used it before!

2 issues:
1) I looked the PBIX file and couldn't figure out how you made [family_id] = 204 go away???
    I changed Roger Hogan's [user_id] to "12" and I thought 204 would show up in the output after refresh, but it didn't?

2) Sample Data error:
the [marital_status] for [user_id]=10 should be "single."  The idea is that 3 or more people could have the same [family_id] because of kids, but  only 2 will have "Married" so I know which two names to  concatenate.  So I think varValidDonors needs to filter on [marital_status] = "Married." 
I tried to make two VARs: 1 for "Married" and 1 for "Single" and use UNION to combine them, but my syntax was wrong.  Besides, you'll probably have a better method...

If you can tweak that 2nd issue, I'm all good! 

 

Thanks again!!!

Please don't delete your response again @bmurf - I just lost  huge amout of typing.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

I'm not typing all of my stuff again. in summary, i changed roger to 12, but there is no donor data in 12 so nothign shows up.

Donors =
VAR varCurrentFamily =
    SUMMARIZE(
        'Donor Table',
        'Donor Table'[family_id  ],
        'Donor Table'[user_id  ],
        'Donor Table'[Marital Status]
    )
VAR varMemberCount =
    COUNTROWS( varCurrentFamily )
VAR varValidDonors =
    IF(
        varMemberCount > 2,
        CONCATENATEX(
            CALCULATETABLE(
                'Donor Table',
                'Donor Table'[Marital Status] = "Married"
            ),
            [first_name       ],
            ", ",
            [first_name       ], ASC
        ),
        CONCATENATEX(
            'Donor Table',
            'Donor Table'[first_name       ],
            ", ",
            'Donor Table'[first_name       ], ASC
        )
    )
RETURN
    IF(
        ISINSCOPE( 'Donor Table'[family_id  ] )
            && [Total Giving]
                <> BLANK(),
        varValidDonors,
        BLANK()
    )

This measure will look for families with more than 2. If it finds that, it only pulls married, otherwise it does the normal stuff. the PBIX file I linked to above can be used again. It has this new code. There is probably a more efficient way to do this, but this works.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
bmurf
Frequent Visitor

Awesome, Thanks!!!
Sorry about the delete!😞

Great @bmurf - glad I was able to assist here!



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
bmurf
Frequent Visitor

Thanks!  I'm pretty sure it's what I need - just trying to follow the logic of the code (particularly isinscope!).
Doing a little research, but THANKS for the fast reply!  I'll accept the solution shortly!

INSCOPE just prevents the measure from reporting names on the TOTAL row. The Family ID field is not "in scope" on the total row, so it will return blank for you if your visual has a total row. If it doesn't this doesn't do anything and isn't necessary.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

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