Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Compare 2 Table and calculate

Hi,

I Have 2 Table,

Table-A

DateUser IDType
27-Dec-222211Active
27-Dec-223441Active
07-Dec-223441Active
27-Jan-232211Active
02-Jan-231154Active
02-Jan-233211Active
16-Jan-233211Active
22-Jan-235467Active
12-Feb-231233Active
21-Feb-233456Active
21-Feb-233211Active
16-Feb-231154Active

and

Table-B

DateUser IDType
27-Dec-221154Passport
27-Dec-223441Passport
07-Dec-223441Passport
27-Jan-231154Passport
02-Jan-231154Passport
02-Jan-233211Passport
16-Jan-233211Passport
22-Jan-235467Passport
12-Feb-231154Passport
21-Feb-233456Passport
21-Feb-233211Passport
16-Feb-233211Passport


Need to calculate ,

Numerator - Distinct count of User ID in Table A that matches with Prev month of User ID from Table B

Denominator - Prev month Distinct count of user ID from table B

Result need come like below,

 

DateNumeratorDenominatorRet(Num/Den)
Dec   
Jan120.5
Feb230.666666667

 

amitchandak 

Ashish_Mathur 

Anonymous 

 

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Anonymous ,

     

    Below is my solution. And also upload my attachment for your reference.

    1.Create a calendar table.

    Table = CALENDAR(DATE(2022,12,1),DATE(2023,3,1))

    2.Create the yearmonth column both in two tables.

    YearMonth = FORMAT([Date],"YYYY-MMM")

    3.Create measures. The first measure is to obtain the User ID in Table B for the previous month that matches the current month in Table A.

    Measure = 
    var _start=EOMONTH(MAX('Table-A'[Date]),-2)+1
    var _end=EOMONTH(MAX('Table-A'[Date]),-1)
    return
    CALCULATE(MAX('Table-B'[User ID]),FILTER(ALL('Table-B'),[User ID]=MAX('Table-A'[User ID])&&[Date]>=_start&&[Date]<=_end))
    Numerator = CALCULATE(DISTINCTCOUNT('Table-A'[User ID]),FILTER(ALLSELECTED('Table-A'),[YearMonth]=MAX('Table-A'[YearMonth])&&[User ID]=[Measure]))
    Denominator = 
    var _start=EOMONTH(MAX('Table-A'[Date]),-2)+1
    var _end=EOMONTH(MAX('Table-A'[Date]),-1)
    return
    CALCULATE(DISTINCTCOUNT('Table-B'[User ID]),FILTER(ALL('Table-B'),[Date]>=_start&&[Date]<=_end))
    Ret(Num/Den) = DIVIDE([Numerator],[Denominator])

    4.Here's the result.

     

     

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.           

4 Replies

  • Hi Anonymous ,

    1-In your Table-A Create a calculated column:

    YearMOnthIndex = YEAR('Table-A'[Date])*12+MONTH('Table-A'[Date])

     

    2-In your Table-B Create a similar calculated column:

    YearMOnthIndex = YEAR('Table-B'[Date])*12+MONTH('Table-B'[Date])

     

    3-Create a measure:

    Numerator =
    CALCULATE (
        DISTINCTCOUNT ( 'Table-A'[User ID] ),
        FILTER (
            CROSSJOIN ( 'TABLE-A', 'Table-B' ),
            'Table-A'[YearMOnthIndex] = 'Table-B'[YearMOnthIndex] + 1
                && 'Table-A'[User ID] = 'Table-B'[User ID]
        )
    )

     

    4-Create a measure: 

    Denominatorr = DISTINCTCOUNT('Table-B'[User ID])

     

    Then you can have their proportion as well.

     

    If this answer solves your problem, give it a thumbs up and mark it as an accepted solution so the others would find what they need easier.

    Regards,
    Loran

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Loran,

       

      Thanks for Replying,

      This is not working. Instead showing blank value.

      Can you try with the sample data which i mentioned above and share me pbix file,if possible.

       

      Result needs to come like above which i mentioned.

       

      Thanks,

       

      MohammadLoran25 

      • MohammadLoran25's avatar
        MohammadLoran25
        Icon for Solution Sage rankSolution Sage

        Anonymous 

        Yes I tried it based on your sample data and it worked.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    Below is my solution. And also upload my attachment for your reference.

    1.Create a calendar table.

    Table = CALENDAR(DATE(2022,12,1),DATE(2023,3,1))

    2.Create the yearmonth column both in two tables.

    YearMonth = FORMAT([Date],"YYYY-MMM")

    3.Create measures. The first measure is to obtain the User ID in Table B for the previous month that matches the current month in Table A.

    Measure = 
    var _start=EOMONTH(MAX('Table-A'[Date]),-2)+1
    var _end=EOMONTH(MAX('Table-A'[Date]),-1)
    return
    CALCULATE(MAX('Table-B'[User ID]),FILTER(ALL('Table-B'),[User ID]=MAX('Table-A'[User ID])&&[Date]>=_start&&[Date]<=_end))
    Numerator = CALCULATE(DISTINCTCOUNT('Table-A'[User ID]),FILTER(ALLSELECTED('Table-A'),[YearMonth]=MAX('Table-A'[YearMonth])&&[User ID]=[Measure]))
    Denominator = 
    var _start=EOMONTH(MAX('Table-A'[Date]),-2)+1
    var _end=EOMONTH(MAX('Table-A'[Date]),-1)
    return
    CALCULATE(DISTINCTCOUNT('Table-B'[User ID]),FILTER(ALL('Table-B'),[Date]>=_start&&[Date]<=_end))
    Ret(Num/Den) = DIVIDE([Numerator],[Denominator])

    4.Here's the result.

     

     

    Best Regards,

    Stephen Tao

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.