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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Aryan_S
Frequent Visitor

Use Var as a table index

Hi,

 

I want to use a variable to sum different columns of a table in a measure.
Instead of doing: 

SUM(Sales[01]) or SUM(Sales[02]) or SUM(Sales[03])

I want to do:
Var index = 01
SUM(Sales[index])

So that I can eventually do:
Var index = *some calculation*
SUM(Sales[index]) 
1 ACCEPTED SOLUTION

@Aryan_S 
This is a good solution given that your dealing with only 12 columns. In cases when you need to deal with un-pivoted data of 50 or 100 columns there is another solution which is way much more complex than yours and won't be feasible in your scenario. I would only simplify the formula as follows:

Daily Budget =
SWITCH (
    MONTH ( TODAY () ),
    01, SUM ( NetSalesBudget[01] ),
    02, SUM ( NetSalesBudget[02] ),
    03, SUM ( NetSalesBudget[03] ),
    04, SUM ( NetSalesBudget[04] ),
    05, SUM ( NetSalesBudget[05] ),
    06, SUM ( NetSalesBudget[06] ),
    07, SUM ( NetSalesBudget[07] ),
    08, SUM ( NetSalesBudget[08] ),
    09, SUM ( NetSalesBudget[09] ),
    10, SUM ( NetSalesBudget[10] ),
    11, SUM ( NetSalesBudget[11] ),
    12, SUM ( NetSalesBudget[12] ),
    "Error"
)

 

 

View solution in original post

3 REPLIES 3
tamerj1
Super User
Super User

Hi @Aryan_S 

The easiest way is to unpivot the table to have one attribute column (equivalent to "index" in your example) and one value column. Not sure if this is a viable possibility in your situation as the formula would be as simple as 

CALCULATE (

SUM ( 'Table'[Value] ),

'Table'[Attribute] = "some calculation"

)

Hello,

Thanks for the response but I won't be able to change any of the underlying tables since they must remain as they currently are. I have found a solution:

Daily Budget =
    VAR Index = FORMAT(TODAY(),"MM")
    Return SWITCH (
         TRUE(),
         Index = "01", SUM(NetSalesBudget[01]),
         Index = "02", SUM(NetSalesBudget[02]),
         Index = "03", SUM(NetSalesBudget[03]),
         Index = "04", SUM(NetSalesBudget[04]),
         Index = "05", SUM(NetSalesBudget[05]),
         Index = "06", SUM(NetSalesBudget[06]),
         Index = "07", SUM(NetSalesBudget[07]),
         Index = "08", SUM(NetSalesBudget[08]),
         Index = "09", SUM(NetSalesBudget[09]),
         Index = "10", SUM(NetSalesBudget[10]),
         Index = "11", SUM(NetSalesBudget[11]),
         Index = "12", SUM(NetSalesBudget[12]),
         "Error"
        )
 
But it is long and messy so was just wondering if there was a cleaner way.

@Aryan_S 
This is a good solution given that your dealing with only 12 columns. In cases when you need to deal with un-pivoted data of 50 or 100 columns there is another solution which is way much more complex than yours and won't be feasible in your scenario. I would only simplify the formula as follows:

Daily Budget =
SWITCH (
    MONTH ( TODAY () ),
    01, SUM ( NetSalesBudget[01] ),
    02, SUM ( NetSalesBudget[02] ),
    03, SUM ( NetSalesBudget[03] ),
    04, SUM ( NetSalesBudget[04] ),
    05, SUM ( NetSalesBudget[05] ),
    06, SUM ( NetSalesBudget[06] ),
    07, SUM ( NetSalesBudget[07] ),
    08, SUM ( NetSalesBudget[08] ),
    09, SUM ( NetSalesBudget[09] ),
    10, SUM ( NetSalesBudget[10] ),
    11, SUM ( NetSalesBudget[11] ),
    12, SUM ( NetSalesBudget[12] ),
    "Error"
)

 

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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