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
villa1980
Resolver II
Resolver II

Multiple Variables in DAX

Hi all,

 

 Not sure if this can be achieved but I have 3 metrics, sales, margin and qty. Currently these are seperate metrics and I was wondering if these can be in one metric using variables?


Thanks

Alex

2 ACCEPTED SOLUTIONS
grazitti_sapna
Super User
Super User

Hi @villa1980,

 

Yes, you can combine calculations for sales, margin, and quantity into a single measure using variables. However, keep in mind that a measure in DAX always returns a single scalar value, so you'll need to decide whether you want to return one of the metrics based on a condition (such as a slicer selection) or concatenate them into a text string.

For example, if you want the user to choose which metric to display using a slicer, you could create a measure like this:

Combined Metric =
VAR SalesMetric = [Sales]
VAR MarginMetric = [Margin]
VAR QtyMetric = [Qty]
VAR SelectedMetric = SELECTEDVALUE('Metric Selector'[Metric]) // a table with options "Sales", "Margin", "Qty"
RETURN
SWITCH(
SelectedMetric,
"Sales", SalesMetric,
"Margin", MarginMetric,
"Qty", QtyMetric,
SalesMetric // default to Sales if nothing is selected
)

Alternatively, if you want to display all three metrics in one card visual as a concatenated string, you could do something like:

Combined Metric Text =
VAR SalesMetric = FORMAT([Sales], "#,##0")
VAR MarginMetric = FORMAT([Margin], "0.00%")
VAR QtyMetric = FORMAT([Qty], "#,##0")
RETURN
"Sales: " & SalesMetric & " | Margin: " & MarginMetric & " | Qty: " & QtyMetric

 

🌟 I hope this solution helps you unlock your Power BI potential! If you found it helpful, click 'Mark as Solution' to guide others toward the answers they need.
💡 Love the effort? Drop the kudos! Your appreciation fuels community spirit and innovation.
🎖 As a proud SuperUser and Microsoft Partner, we’re here to empower your data journey and the Power BI Community at large.
🔗 Curious to explore more? [Discover here].
Let’s keep building smarter solutions together!

View solution in original post

danextian
Super User
Super User

Hi @villa1980 

 

That's possible but you would need  another table containing a column that holds the metric names,  a sort column and a format string.  Just variables without a row that you can assign the values to won't work.
danextian_0-1741341448435.png

 

In the measure below, you can use the actual expressions but I prefer putting them in their own measures for re-usability.

My Metrics =
VAR _Rev = [Total Revenue]
VAR _TXn = [Total Transactions]
VAR _ATR = [Average Rev]
RETURN
    SWITCH (
        SELECTEDVALUE ( Metrics[Metric] ),
        "Revenue", _Rev,
        "Transactions", _TXn,
        "ATR", _ATR
    )

danextian_1-1741342108847.png

The Format String column in the Metrics table will be used to apply the desired formatting to each metric. The above screnshot shows #,#.00 instead of different format for each. While you can use FORMAT function, that will return a text string instead of a number.

danextian_2-1741342246023.png

 

If this is the only thing you're trying to achieve, I would just use Field Parameters intead which is easier to setup and automated. You can access this feature from the Modeling tab, New Parameter then Fields. This creates a calculated table which you can edit if you need to rename the Display Names, chang the sort, add and or delete a measure.

danextian_3-1741342395426.png

danextian_4-1741342532631.png

Please see the attached sample pbix.

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

2 REPLIES 2
danextian
Super User
Super User

Hi @villa1980 

 

That's possible but you would need  another table containing a column that holds the metric names,  a sort column and a format string.  Just variables without a row that you can assign the values to won't work.
danextian_0-1741341448435.png

 

In the measure below, you can use the actual expressions but I prefer putting them in their own measures for re-usability.

My Metrics =
VAR _Rev = [Total Revenue]
VAR _TXn = [Total Transactions]
VAR _ATR = [Average Rev]
RETURN
    SWITCH (
        SELECTEDVALUE ( Metrics[Metric] ),
        "Revenue", _Rev,
        "Transactions", _TXn,
        "ATR", _ATR
    )

danextian_1-1741342108847.png

The Format String column in the Metrics table will be used to apply the desired formatting to each metric. The above screnshot shows #,#.00 instead of different format for each. While you can use FORMAT function, that will return a text string instead of a number.

danextian_2-1741342246023.png

 

If this is the only thing you're trying to achieve, I would just use Field Parameters intead which is easier to setup and automated. You can access this feature from the Modeling tab, New Parameter then Fields. This creates a calculated table which you can edit if you need to rename the Display Names, chang the sort, add and or delete a measure.

danextian_3-1741342395426.png

danextian_4-1741342532631.png

Please see the attached sample pbix.

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
grazitti_sapna
Super User
Super User

Hi @villa1980,

 

Yes, you can combine calculations for sales, margin, and quantity into a single measure using variables. However, keep in mind that a measure in DAX always returns a single scalar value, so you'll need to decide whether you want to return one of the metrics based on a condition (such as a slicer selection) or concatenate them into a text string.

For example, if you want the user to choose which metric to display using a slicer, you could create a measure like this:

Combined Metric =
VAR SalesMetric = [Sales]
VAR MarginMetric = [Margin]
VAR QtyMetric = [Qty]
VAR SelectedMetric = SELECTEDVALUE('Metric Selector'[Metric]) // a table with options "Sales", "Margin", "Qty"
RETURN
SWITCH(
SelectedMetric,
"Sales", SalesMetric,
"Margin", MarginMetric,
"Qty", QtyMetric,
SalesMetric // default to Sales if nothing is selected
)

Alternatively, if you want to display all three metrics in one card visual as a concatenated string, you could do something like:

Combined Metric Text =
VAR SalesMetric = FORMAT([Sales], "#,##0")
VAR MarginMetric = FORMAT([Margin], "0.00%")
VAR QtyMetric = FORMAT([Qty], "#,##0")
RETURN
"Sales: " & SalesMetric & " | Margin: " & MarginMetric & " | Qty: " & QtyMetric

 

🌟 I hope this solution helps you unlock your Power BI potential! If you found it helpful, click 'Mark as Solution' to guide others toward the answers they need.
💡 Love the effort? Drop the kudos! Your appreciation fuels community spirit and innovation.
🎖 As a proud SuperUser and Microsoft Partner, we’re here to empower your data journey and the Power BI Community at large.
🔗 Curious to explore more? [Discover here].
Let’s keep building smarter solutions together!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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