Forum Discussion

H3nning's avatar
H3nning
Helper V
2 years ago
Solved

Distinct count depending on multiple lines

Hi,

 

i have a problem counting the number of customers that are customers for a certain period of time. Important: I dont want to count them once today, but for any given moment in time!

 

I have the following tables:

Contract:

Contract IDCustomerStartEnd
1A1.1.202031.10.2020
2A1.12.202031.03.2021
3A1.1.202031.10.2020
4A1.1.202101.11.2021
5B1.1.202031.03.2020
6B01.09.202131.12.2021

 

Date:

A normal Calendar table with every day as one line

 

Customer:

Customer IDCustomer Data
AName1
BName2

 

I want to build a diagram with date from Date table on x Axis and Number of distinct customers which were customer longer than 1 year at the given time. It is not important, that the customers covered every day during that period, but he has to have a contract in that moment or on at least one day in the given period. 

So the result should be if visualized with granularity month:

MonthDistinctcount Customers longer 1 year
Jan 200
Feb 200
Mar 200
...0
Dez 200
Jan 211 (Customer A)
Feb 211 (Customer A)
Mar 211 (Customer A)
Apr 211 (Customer A)
May211 (Customer A)
Jun 211 (Customer A)
Jul 211 (Customer A)
Aug 211 (Customer A)
Sep 212 (Customer A and B, B is active now an over all older than 1 year)
Oct 212 (Customer A and B, B is active now an over all older than 1 year)
Nov 212 (Customer A and B, B is active now an over all older than 1 year, A has at least one day during that period)
Dec 211 (Customer B, A is not active anymore)

 

Im very curious if someone can figure that out. It should be a measure, so the granularity is dynamic (year, quarter, month, day). Would be great if someone helps out, because i find it really hard to figure out... Thanks!

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi H3nning ,

    Based on your problems, here are my answers.

    First I create three tables as you mentioned.

    Then I write a  measure.

    Formatted Date = FORMAT('Date'[Date], "d.M.yyyy")

    Finally I write another measure and you will get what you want.

    MEASURE =
    VAR CurrentDate =
        SELECTEDVALUE ( 'Date'[Formatted Date] )
    RETURN
        CALCULATE (
            DISTINCTCOUNT ( Contract[Customer] ),
            FILTER (
                'Contract',
                Contract[Start] <= CurrentDate
                    && Contract[End] >= CurrentDate
            )
        )

     

     

     

    Best Regards

    Yilong Zhou

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

     

3 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi H3nning ,

    Based on your problems, here are my answers.

    First I create three tables as you mentioned.

    Then I write a  measure.

    Formatted Date = FORMAT('Date'[Date], "d.M.yyyy")

    Finally I write another measure and you will get what you want.

    MEASURE =
    VAR CurrentDate =
        SELECTEDVALUE ( 'Date'[Formatted Date] )
    RETURN
        CALCULATE (
            DISTINCTCOUNT ( Contract[Customer] ),
            FILTER (
                'Contract',
                Contract[Start] <= CurrentDate
                    && Contract[End] >= CurrentDate
            )
        )

     

     

     

    Best Regards

    Yilong Zhou

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

     

    • H3nning's avatar
      H3nning
      Helper V

      That only counts the numer of customers but not those older than one year. But i could manage that by writing the lowest Starting date of any contract on each customer:

      LowestStart=Min(Start)

      And bringing that to the contract:

      LowestStartOfCustomer=Related(Customer[LowestStart])

       

      That i can include i a datediff clause in the conditions of your measure. So thank you!