Forum Discussion

lchaplen's avatar
lchaplen
Icon for Helper II rankHelper II
4 years ago
Solved

Calculating unique count across two tables values each with conditions.

I have two tables Programs and Benefits.  There are people I need to count uniquely based on conditions in each table

Programs:   person is active = 1 and Program = XYZ and startdate = MM/DD/YYY

Benefits:  person is active = 1 

A person may be in either or both tables and I need to get a unique count of total people from both tables with the above conditions. 

 

I don't want to count only the people in both tables. I need to count uniquely all people.  I could do this in SQL but haven't been able to crack it in Power BI. 

 

  • HI so I cracked it.. one thing to mention in Variables you can't go off another variable.

    TEST HH_Non-Cash_Benefit_w_LIHEAP =
    VAR NonCasha =
    CALCULATETABLE(VALUES('rpt vw_Client_Households'[Household_Code]),FILTER('rpt vw_Client_Non_Cash_Benefits','rpt vw_Client_Non_Cash_Benefits'[Client_Active] = 1 ))
    VAR LIHEAPa =
    CALCULATETABLE(VALUES('rpt vw_Client_Households'[Household_Code]),'rpt vw_Client_Programs'[Program] = "Low Income Heating Assistance")
    VAR Combined_NonCash_LIHEAP = UNION(NonCasha,LIHEAPa)
    VAR Unique_NCandLIHEAP = DISTINCT(Combined_NonCash_LIHEAP)
    RETURN
    Countrows(Unique_NCandLIHEAP)

3 Replies

  • If you give someone a fish then you only give them one meal, but if you teach them how to fish then they can feed themselves and teach others for a lifetime.  I prefer to teach members on this forum techniques rather give full solutions and do their job. You can then adapt the technique for your solution, learn some DAX skills for next time and soon become a Power BI Super User like me. ðŸ˜€


    Click here to down load an example 

    The Program table with people and colours (red, green).
    The Benefits table with people and colours (small, medium, large).
    The answer has the number of red or small people.

    You should be able to easily learn the technique and adapt it to your solution from this example 

     

    Red or Small People =
    VAR Red_People = CALCULATETABLE(VALUES(Program[Person]),Program[Colour] = "Red")
    VAR Small_People = CALCULATETABLE(VALUES(Benefits[Person]),Benefits[Size] = "Small")
    VAR Combined_Red_Small_People = UNION(Red_People,Small_People)
    VAR Unique_Red_Small_People = DISTINCT(Combined_Red_Small_People)
    RETURN
    COUNTROWS(Unique_Red_Small_People)
     

    Thanks for reaching out for help.

    I put in a lot of effort to help you, now please quickly help me by giving kudos.

    Remeber we are unpaid volunteers and here to coach you with Power BI and DAX skills and ttechniques, not do the users job for them !

    So plaase click the thumbs up and accept as solution button. 

    One question per ticket please. If you need to extend your request then please raise a new ticket.

    You will get a quicker response and each volunteer solver will get the kudos they deserve. Thank you ! 

    • lchaplen's avatar
      lchaplen
      Icon for Helper II rankHelper II

      Well it is getting me closer.. this is my formula.. Note the Person code is derived from a 3rd table. I'm getting the error:  "A function 'PLACEHOLDER' has been used in a TRUE/FALSE expression that is used as a table filter expresssion.  This is not allowed" 

       

      TEST HH_Non-Cash_Benefit_w_LIHEAP =
      VAR NonCash =
      CALCULATETABLE(VALUES('rpt vw_Client_Households'[Household_Code]),not(isblank('rpt vw_Client_Household_Employment'[HH Non_Cash])))
      VAR LIHEAP =
      CALCULATETABLE(VALUES('rpt vw_Client_Households'[Household_Code]),'rpt vw_Client_Programs'[Program] = "Low Income Heating Assistance")
      VAR Combined_NonCash_LIHEAP = UNION(NonCash,LIHEAP)
      VAR Unique_NCandLIHEAP = DISTINCT(Combined_NonCash_LIHEAP)
      RETURN
      COUNTROWS(Unique_NCandLIHEAP)
      • lchaplen's avatar
        lchaplen
        Icon for Helper II rankHelper II

        HI so I cracked it.. one thing to mention in Variables you can't go off another variable.

        TEST HH_Non-Cash_Benefit_w_LIHEAP =
        VAR NonCasha =
        CALCULATETABLE(VALUES('rpt vw_Client_Households'[Household_Code]),FILTER('rpt vw_Client_Non_Cash_Benefits','rpt vw_Client_Non_Cash_Benefits'[Client_Active] = 1 ))
        VAR LIHEAPa =
        CALCULATETABLE(VALUES('rpt vw_Client_Households'[Household_Code]),'rpt vw_Client_Programs'[Program] = "Low Income Heating Assistance")
        VAR Combined_NonCash_LIHEAP = UNION(NonCasha,LIHEAPa)
        VAR Unique_NCandLIHEAP = DISTINCT(Combined_NonCash_LIHEAP)
        RETURN
        Countrows(Unique_NCandLIHEAP)