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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
manojk_pbi
Helper V
Helper V

DAX Measure for Data Aggregate for current and previous month

Hi 

 

I have one requirement to show aggregated data in the form of KPI. Attached the sample data and expected out put below.

 

Please suggest the good approach to acheive the result.

MYProject NameOTRCPDOTR*CPDDelayCost
25-JunPRJ1100%0.770.770168
25-JunPRJ20%0.000.00012
25-JunPRJ3100%1.981.9800
25-JunPRJ4100%1.561.5600
25-JunPRJ5100%1.941.9400
25-JunPRJ6100%0.660.6600
25-JunPRJ743%3.001.302030
25-JunPRJ887%2.522.19590
25-JunPRJ9100%10.7410.7400
25-MayPRJ193%0.730.68300
25-MayPRJ2100%5.195.1900
25-MayPRJ373%0.800.58153222
25-MayPRJ4100%0.640.6400
25-MayPRJ585%1.040.88660
25-MayPRJ6100%1.061.0600
25-MayPRJ785%2.351.99660
25-MayPRJ885%1.371.16660
25-MayPRJ979%2.171.711831796

OTR = sum(OTR*CPD)/sum(CPD)

Delay = AVG(Delay)

manojk_pbi_0-1755601343274.png

 

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @manojk_pbi ,

 

You can achieve this result by creating a series of DAX measures and then arranging them in a Matrix visual using a helper table.

First, it is essential that you have a proper date column in your model. Time intelligence functions like DATEADD, which you'll need for the "Previous Month" calculation, require a valid date data type to work correctly. Text values like "25-Jun" should be converted into a date, for example 6/1/2025, which can be done easily in Power Query.

Once your dates are set up, you should create base measures for your core calculations. This is a good practice that makes your DAX model easier to read and manage. Create one measure for the weighted average OTR and another for the average Delay. Remember to replace 'YourTable' with the actual name of your data table.

OTR_Base =
DIVIDE(
    SUMX('YourTable', 'YourTable'[OTR] * 'YourTable'[CPD]),
    SUM('YourTable'[CPD])
)
Delay_Base =
AVERAGE('YourTable'[Delay])

With these base measures, you can now create the specific measures for the Current Month (CM), Previous Month (PM), and the difference. The PM calculation uses the CALCULATE function to modify the filter context, telling Power BI to perform the calculation on the date range shifted back by one month using DATEADD. The difference is a simple subtraction.

// --- OTR Measures ---
OTR KPI CM = [OTR_Base]

OTR KPI PM =
CALCULATE(
    [OTR_Base],
    DATEADD('YourTable'[MONTHYEAR], -1, MONTH)
)

OTR Diff = [OTR KPI CM] - [OTR KPI PM]
// --- Delay Measures ---
Delay KPI CM = [Delay_Base]

Delay KPI PM =
CALCULATE(
    [Delay_Base],
    DATEADD('YourTable'[MONTHYEAR], -1, MONTH)
)

Delay Diff = [Delay KPI CM] - [Delay KPI PM]

After creating these, be sure to format them correctly from the Measure tools ribbon—set the OTR measures to Percentage and the Delay measures to Whole Number. To get the exact visual layout, you will use a Matrix. This requires creating a small, disconnected table to define the row structure. Use the Enter Data feature on the Home ribbon to create a table (you can call it KPI Structure) with columns named Category, Metric, and SortOrder to list out each row you want to see ("OTR", "KPI CM", etc.).

With that helper table created, you need one final measure that acts as a "switcher." This measure will check which row of the matrix is being calculated and show the corresponding measure you already created. This pattern is very powerful for creating custom financial or KPI reports.

KPI Value =
VAR SelectedCategory = SELECTEDVALUE('KPI Structure'[Category])
VAR SelectedMetric = SELECTEDVALUE('KPI Structure'[Metric])
RETURN
SWITCH(
    TRUE(),
    SelectedCategory = "OTR"   && SelectedMetric = "KPI CM", [OTR KPI CM],
    SelectedCategory = "OTR"   && SelectedMetric = "KPI PM", [OTR KPI PM],
    SelectedCategory = "OTR"   && SelectedMetric = "Diff", [OTR Diff],
    SelectedCategory = "Delay" && SelectedMetric = "KPI CM", [Delay KPI CM],
    SelectedCategory = "Delay" && SelectedMetric = "KPI PM", [Delay KPI PM],
    SelectedCategory = "Delay" && SelectedMetric = "Diff", [Delay Diff],
    BLANK()
)

To finish, place a Matrix visual on your report. Drag the Category and Metric columns from your KPI Structure table to the Rows field, and drag your new [KPI Value] measure to the Values field. Add a slicer for your date column, and your visual will now dynamically show the current and previous month values based on the selection.

 

Best regards,

View solution in original post

4 REPLIES 4
v-venuppu
Community Support
Community Support

Hi @manojk_pbi ,

Thank you for reaching out to Microsoft Fabric Community.

Thank you @DataNinja777 for the prompt response.

I wanted to check if you had the opportunity to review the information provided and resolve the issue..?Please let us know if you need any further assistance.We are happy to help.

Thank you.

DataNinja777
Super User
Super User

Hi @manojk_pbi ,

 

You can achieve this result by creating a series of DAX measures and then arranging them in a Matrix visual using a helper table.

First, it is essential that you have a proper date column in your model. Time intelligence functions like DATEADD, which you'll need for the "Previous Month" calculation, require a valid date data type to work correctly. Text values like "25-Jun" should be converted into a date, for example 6/1/2025, which can be done easily in Power Query.

Once your dates are set up, you should create base measures for your core calculations. This is a good practice that makes your DAX model easier to read and manage. Create one measure for the weighted average OTR and another for the average Delay. Remember to replace 'YourTable' with the actual name of your data table.

OTR_Base =
DIVIDE(
    SUMX('YourTable', 'YourTable'[OTR] * 'YourTable'[CPD]),
    SUM('YourTable'[CPD])
)
Delay_Base =
AVERAGE('YourTable'[Delay])

With these base measures, you can now create the specific measures for the Current Month (CM), Previous Month (PM), and the difference. The PM calculation uses the CALCULATE function to modify the filter context, telling Power BI to perform the calculation on the date range shifted back by one month using DATEADD. The difference is a simple subtraction.

// --- OTR Measures ---
OTR KPI CM = [OTR_Base]

OTR KPI PM =
CALCULATE(
    [OTR_Base],
    DATEADD('YourTable'[MONTHYEAR], -1, MONTH)
)

OTR Diff = [OTR KPI CM] - [OTR KPI PM]
// --- Delay Measures ---
Delay KPI CM = [Delay_Base]

Delay KPI PM =
CALCULATE(
    [Delay_Base],
    DATEADD('YourTable'[MONTHYEAR], -1, MONTH)
)

Delay Diff = [Delay KPI CM] - [Delay KPI PM]

After creating these, be sure to format them correctly from the Measure tools ribbon—set the OTR measures to Percentage and the Delay measures to Whole Number. To get the exact visual layout, you will use a Matrix. This requires creating a small, disconnected table to define the row structure. Use the Enter Data feature on the Home ribbon to create a table (you can call it KPI Structure) with columns named Category, Metric, and SortOrder to list out each row you want to see ("OTR", "KPI CM", etc.).

With that helper table created, you need one final measure that acts as a "switcher." This measure will check which row of the matrix is being calculated and show the corresponding measure you already created. This pattern is very powerful for creating custom financial or KPI reports.

KPI Value =
VAR SelectedCategory = SELECTEDVALUE('KPI Structure'[Category])
VAR SelectedMetric = SELECTEDVALUE('KPI Structure'[Metric])
RETURN
SWITCH(
    TRUE(),
    SelectedCategory = "OTR"   && SelectedMetric = "KPI CM", [OTR KPI CM],
    SelectedCategory = "OTR"   && SelectedMetric = "KPI PM", [OTR KPI PM],
    SelectedCategory = "OTR"   && SelectedMetric = "Diff", [OTR Diff],
    SelectedCategory = "Delay" && SelectedMetric = "KPI CM", [Delay KPI CM],
    SelectedCategory = "Delay" && SelectedMetric = "KPI PM", [Delay KPI PM],
    SelectedCategory = "Delay" && SelectedMetric = "Diff", [Delay Diff],
    BLANK()
)

To finish, place a Matrix visual on your report. Drag the Category and Metric columns from your KPI Structure table to the Rows field, and drag your new [KPI Value] measure to the Values field. Add a slicer for your date column, and your visual will now dynamically show the current and previous month values based on the selection.

 

Best regards,

Thanks @DataNinja777 , the solution working as expected. Very well explained.

manojk_pbi
Helper V
Helper V

Hello @v-ssriganesh , i have one more requirement. 

Looking for your guidance here

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.