Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Abrar786
Helper I
Helper I

RFM Analysis Based on Year and Month Selection

Hi,

I am trying to create a dynamic RFM analysis based on the Year and Month selected from a slicer. The logic is: if Year = 2025 and Month = April are selected, then go back two years from the selected period, retrieve all the transactions within that range, and perform the RFM calculation on that data. I was able to achieve this using a measure, and the following table works as expected.

Abrar786_0-1744143910723.png

But now I want to create a dashboard that shows the total number of customers and their sales by each Sub_Category. However, I'm not able to get the correct result and need help.

I have already created a measure that calculates everything perfectly, but for some reason, when I use the measure in a visual, it doesn’t return the correct results.

 

Invoice Table.pngCategory Table.pngThis is current output which is not correct.This is current output which is not correct.Final Result should be like this.Final Result should be like this.

 
[Measure] Customer Count by RFM SubCategory =
VAR EndDate      = [Selected Date]
VAR StartDate    = [Last2Years Start Date] -- 24 months back
VAR selected_coverage_model = [Selected Coverage Model]
VAR selected_coverage_model_default = [Selected Coverage Model (Default Value)]

-- Step 1: Filter invoices in the rolling 24-month window
VAR CustomerTable =
    ADDCOLUMNS(
        SUMMARIZE(
            FILTER(
                'Invoice Header',
                'Invoice Header'[invoice_date] >= StartDate &&
                'Invoice Header'[invoice_date] <= EndDate &&
                (
                    ISBLANK(selected_coverage_model) ||
                    'Invoice Header'[salesforce_coverage_model_combined] = selected_coverage_model
                )
            ),
            'Invoice Header'[salesforce_id_customer_id_combined]
        ),
        "Recency",
            CALCULATE(
                DATEDIFF(
                    CALCULATE(
                        MAX('Invoice Header'[invoice_date]),
                        FILTER(ALL('Calendar'),'Calendar'[Date] >= StartDate && 'Calendar'[Date] <= EndDate)
                    ),
                    EndDate,
                    DAY
                )
            ),
        "Frequency",
            CALCULATE(
                DISTINCTCOUNT('Invoice Header'[invoice_number]),
                FILTER(ALL('Calendar'),'Calendar'[Date] >= StartDate && 'Calendar'[Date] <= EndDate)
            ),
        "Monetary",
            CALCULATE(
                SUM('Invoice Detail'[net_line_amount_cad]),
                FILTER(ALL('Calendar'),'Calendar'[Date] >= StartDate && 'Calendar'[Date] <= EndDate)
            )
    )

-- Step 2: Bucket R, F, M into 1-5 scale (example using percentiles)
VAR RFM_Scored =
    ADDCOLUMNS(
        CustomerTable,
        "R_Score",
        MAXX
        (
            FILTER(
                'RFM Criteria',
                [Recency] >= 'RFM Criteria'[R Criteria (Min)] &&
                [Recency] <= 'RFM Criteria'[R Criteria (Max)] &&
                'RFM Criteria'[Coverage Model] = selected_coverage_model_default
            ),
            'RFM Criteria'[Score]
        ),
        "F_Score",
         MAXX
         (
            FILTER(
                'RFM Criteria',
                [Frequency] >= 'RFM Criteria'[F Criteria (Min)] &&
                [Frequency] < 'RFM Criteria'[F Criteria (Max)] &&
                'RFM Criteria'[Coverage Model] = selected_coverage_model_default
            ),
            'RFM Criteria'[Score]
        ),
        "M_Score",
         MAXX
         (
            FILTER(
                'RFM Criteria',
                [Monetary] >= 'RFM Criteria'[M Criteria (Min)] &&
                [Monetary] < 'RFM Criteria'[M Criteria (Max)] &&
                'RFM Criteria'[Coverage Model] = selected_coverage_model_default
            ),
            'RFM Criteria'[Score]
        )
    )

-- Step 3: Concatenate into 3-digit RFM score
VAR RFM_Categorized =
    ADDCOLUMNS(
        RFM_Scored,
        "RFM_Score",
        [R_Score] * 100 + [F_Score] * 10 + [M_Score]
           
    )

-- Step 4: Join with mapping table to get Sub_Category


VAR WithSubCategory =    
    ADDCOLUMNS(
        RFM_Categorized,
        "Sub_Category",
            LOOKUPVALUE(
                'RFM Categories'[Sub_Category],
                'RFM Categories'[RFM Score],
                [RFM_Score]
            )        
    )
 
VAR WithCategory =
       ADDCOLUMNS(
        WithSubCategory,
        "Category",
            LOOKUPVALUE(
            'Categories'[Category],
            'Categories'[Sub_Category],
            [Sub_Category]
            )
     )


-- Step 5: Return count of customers in each Sub_Category (driven by visual context)

RETURN


CALCULATE(
    COUNTROWS(
        FILTER(
                    WithCategory,
                    [Sub_Category] IN VALUES('Categories'[Sub_Category])
                )
    ),KEEPFILTERS('Categories')  
)


1 ACCEPTED SOLUTION

The only change I made to my previous measure is shown below:

CALCULATETABLE(
'Invoice Header',
REMOVEFILTERS('Calendar'),
REMOVEFILTERS('Invoice Header')

I used CALCULATETABLE to remove slicer filters so that the measure controls everything, not the visuals—and it worked.

 

Thank you.

View solution in original post

6 REPLIES 6
Abrar786
Helper I
Helper I

@v-veshwara-msft ,

Thank you for your help. I’ve already fixed the measure, and it’s working now. The issue was with the filter—I fixed it, and everything is working properly.

Great to hear that the issue was with the filter and that it's now resolved and the measure is working as expected.

If you don’t mind, could you please mark your reply as the “Accepted Solution”? Also, if possible, sharing a brief summary of the fix would be really helpful for others who might face a similar issue in the future.

Thanks again for following up, and feel free to reach out if you need any further assistance.

The only change I made to my previous measure is shown below:

CALCULATETABLE(
'Invoice Header',
REMOVEFILTERS('Calendar'),
REMOVEFILTERS('Invoice Header')

I used CALCULATETABLE to remove slicer filters so that the measure controls everything, not the visuals—and it worked.

 

Thank you.

v-veshwara-msft
Community Support
Community Support

Hi @Abrar786 ,

Thanks for sharing the detailed measure and screenshots. 

Your DAX logic for dynamic RFM scoring appears well-structured and should work correctly in isolation. However, since the visual isn’t returning the expected results, a few possible causes could be:

1. Filter Context Mismatch:
The measure builds a variable with Sub_Category, but if your visual uses 'Categories'[Sub_Category], there could be misalignment unless a proper relationship or matching context is established.

 

2. Missing Mappings in Lookup Tables:
If any RFM score (like 431, 555, etc.) doesn't exist in 'RFM Categories', the LOOKUPVALUE function will return blank. This causes those rows to be excluded from the final count.

 

3. Coverage Model Filtering:
If your selected Coverage Model from the slicer isn't present across all related tables, it may return zero rows during filtering.

 

If the issue still persists after trying the above steps, could you please share a sample of your data (no confidential info needed) to assist you better and reproduce this on our side. 

This will help us validate the end-to-end logic and provide accurate guidance according to your model.

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly and a kudos would be appreciated.

Thank you.

Hi @v-veshwara-msft ,

Here is the .pbix file.
Please note that the current results in the file are not correct. The expected output should match what is shown below.

 

https://drive.google.com/file/d/1dpKP-JffVUG5DFIF_aUcgV_ioOtSDeQg/view?usp=drive_link

 

Expected Output.png

Thank you.

Hi @Abrar786 ,
I’m unable to access the file and have submitted a request. Could you please approve the access?

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors