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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

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
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.