Forum Discussion

rhinorocks's avatar
rhinorocks
Frequent Visitor
4 years ago
Solved

Defining Variables

Hi All,

 

I am new to Dax and currently facing issues in defining variables. 

 

Can we define table columns in Variable? Like I have a Table called Customer Master and I want to define a variable storing the customer ID. But the error is coming as Var is expecting a scalar value? 

 

Kindly help!!

  • rhinorocks's avatar
    rhinorocks
    4 years ago

    I think I got the solution of this issue. I was trying to write a measure instead of the calculated column. 

5 Replies

  • Syk's avatar
    Syk
    Resident Rockstar

    You can work with columns in DAX but your measure has to return a scalar value. For example, in the dax below I'm storing a column in a variable but returning the sum of the column. If you reply with your specific use case, we may be able to help in more detail!

    measure = 
    var sales = 'sales'[sales_amount]
    
    return sum(sales)

     

    • krzlag's avatar
      krzlag
      New Member

      Hi Syk, I build similar logic, but it doesn't work


      Syk wrote:

      You can work with columns in DAX but your measure has to return a scalar value. For example, in the dax below I'm storing a column in a variable but returning the sum of the column. If you reply with your specific use case, we may be able to help in more detail!

       

      measure = 
      var sales = 'sales'[sales_amount]
      
      return sum(sales)

       

       


       

  • rhinorocks's avatar
    rhinorocks
    Frequent Visitor

    Consider the following problem posted on this forum - 

     

    https://community.powerbi.com/t5/Desktop/Days-between-two-dates-in-different-rows/m-p/2367394#M851834

     

    When I am trying to write this particular part of the code - 

     

    Days between pricelists =
    var _item = 'Table'[Item]
     
    The tellisense is not able to detect this 'Table' hence I am not being able to proceed. Check the attached picture
    While If I write the second line of the soluation, then I am getting no error. 
     
    var _endDate = 'Table'[ValidityEndingDate]
     
    Why is this happening?

     

    • rhinorocks's avatar
      rhinorocks
      Frequent Visitor

      I think I got the solution of this issue. I was trying to write a measure instead of the calculated column.