Forum Discussion

KimGriso's avatar
KimGriso
New Member
11 months ago
Solved

Customer buying every year

Hi there, 

I am new learner and I am stuggling on trying to find out revenue from customers buying products every year. 

Could someone help me with my senario please? 

For example below table is sales data during 2022- 2024, how can I find out Marie who  bought every year and her purchase amout?  

 

Customer NameOrder DateOrder QtySales Amt
Marie11/23/20222100
Marie12/28/20225250
Marie02/16/20234200
Marie04/28/2024150
Julie09/18/20238400
Julie11/02/20233150
Julie06/12/20245250
Julie08/22/20242100
Julie11/05/20247350

 

Thanks in advance. 

 

  • Hi KimGriso 

    This is a measure only approach:

    Customer Sales with all years = 
    VAR YearsWithDupes =
        // Build a table of all order dates and customers, with a computed "Year" column
        SUMMARIZE (
            ALL ( 'Table' ),                          // remove filters, work over entire table
            'Table'[Order Date],
            'Table'[Customer Name],
            "Year", YEAR ( 'Table'[Order Date] )
        )
    VAR AllYears =
        // Count how many distinct years exist in the whole table
        COUNTROWS ( GROUPBY ( YearsWithDupes, [Year] ) )
    RETURN
        SUMX (
            FILTER (
                // Build a per-customer table: one row per customer with sales and year count
                SUMMARIZECOLUMNS (
                    'Table'[Customer Name],
                    "@amount", SUM ( 'Table'[Sales Amt] ),   // total sales per customer
                    "@CustomerYears",
                        COUNTROWS (
                            GROUPBY (
                                FILTER (
                                    YearsWithDupes,
                                    [Customer Name] IN VALUES ( 'Table'[Customer Name] ) // only that customer
                                ),
                                [Year]    // group by year
                            )
                        ) // count distinct years for that customer
                ),
                // Keep only customers whose distinct years match the overall distinct years
                [@CustomerYears] = AllYears
            ),
            [@amount]   // sum the sales of the qualifying customers
        )

    There are other approaches in the attached pbix that requires a dedicated tables and helper columns in the fact table.

    Performance difference amount these approaches is neglible with small tables but can be obvious otherwise especially for the approach above. 

     

9 Replies

  • Hi,

    I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

    Please check the below picture and the attached pbix file.

     

     

     

     

    Who bought every year: = 
    VAR _year =
        ALL ( 'Calendar'[Year] )
    VAR _allyearcount =
        COUNTROWS ( _year )
    VAR _sales =
        FILTER (
            ADDCOLUMNS ( _year, "@salesamount", CALCULATE ( SUM ( sales[Sales Amt] ) ) ),
            [@salesamount] <> BLANK ()
        )
    VAR _count =
        COUNTROWS ( _sales )
    RETURN
        IF ( _allyearcount == _count, "Yes", "No" )

     

     

    Who bought every year sales amount: = 
    VAR _year =
        ALL ( 'Calendar'[Year] )
    VAR _allyearcount =
        COUNTROWS ( _year )
    VAR _sales =
        FILTER (
            ADDCOLUMNS ( _year, "@salesamount", CALCULATE ( SUM ( sales[Sales Amt] ) ) ),
            [@salesamount] <> BLANK ()
        )
    VAR _count =
        COUNTROWS ( _sales )
    RETURN
        IF ( _allyearcount == _count, SUM(sales[Sales Amt] ) )

     

    • KimGriso's avatar
      KimGriso
      New Member

      Hello Mathur,

      Thanks for your support but it seems not work at my side as the result shows all customers and revenue.

      Like below screenshort, my sales data including 03 years 2015, 2016, 2017 so this customer should not be in the list of customer who buy every year because he only bought 2016, 2017. 

       

       

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Icon for Super User rankSuper User

        i do not know what mistake you are committing.  As can be seen very clearly, it works fine on my file.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi KimGriso ,
    I would also take a moment to thank Jihwan_Kim 
     , for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.
     

    I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi KimGriso ,

      I hope the above details help you fix the issue. If you still have any questions or need more help, feel free to reach out. We’re always here to support you

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi KimGriso ,
        I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions.

  • Hi KimGriso 

    This is a measure only approach:

    Customer Sales with all years = 
    VAR YearsWithDupes =
        // Build a table of all order dates and customers, with a computed "Year" column
        SUMMARIZE (
            ALL ( 'Table' ),                          // remove filters, work over entire table
            'Table'[Order Date],
            'Table'[Customer Name],
            "Year", YEAR ( 'Table'[Order Date] )
        )
    VAR AllYears =
        // Count how many distinct years exist in the whole table
        COUNTROWS ( GROUPBY ( YearsWithDupes, [Year] ) )
    RETURN
        SUMX (
            FILTER (
                // Build a per-customer table: one row per customer with sales and year count
                SUMMARIZECOLUMNS (
                    'Table'[Customer Name],
                    "@amount", SUM ( 'Table'[Sales Amt] ),   // total sales per customer
                    "@CustomerYears",
                        COUNTROWS (
                            GROUPBY (
                                FILTER (
                                    YearsWithDupes,
                                    [Customer Name] IN VALUES ( 'Table'[Customer Name] ) // only that customer
                                ),
                                [Year]    // group by year
                            )
                        ) // count distinct years for that customer
                ),
                // Keep only customers whose distinct years match the overall distinct years
                [@CustomerYears] = AllYears
            ),
            [@amount]   // sum the sales of the qualifying customers
        )

    There are other approaches in the attached pbix that requires a dedicated tables and helper columns in the fact table.

    Performance difference amount these approaches is neglible with small tables but can be obvious otherwise especially for the approach above.