Forum Discussion

DSR's avatar
DSR
Resolver I
1 year ago

Does Excel 2016 Pro Dax does not accept Variable Only HardCode? It is giving me Error

Hello, Your help is appreciated.

I loaded the tables intio the DataModel:

Q1-     fVotesAmioun2016

Q2-     dCandidatesAmioun2016

Q-3     dRegionAmioun2016

Q4-     CandidateCompared      //is a variable (Text)(I want it Dymamic, I change the candidate, it changes in the Variable. its codes are:

let
Source = Excel.CurrentWorkbook(){[Name="CandidateCompared_Criteria"]}[Content],
ExtractCell.asText = Source[CandidateCompared_Criteria]{0}
in
ExtractCell.asText

and get the name Get the Name 

 

Linked them in the data model 

 

Measure1:   

Study Candidate Votes in Sharqi:=CALCULATE([Tt Votes],fVotesAmioun2016[CandidateEN]=CandidateCompared,fVotesAmioun2016[Region]="Sharqi") giving me error

Study Candidate Votes in Sharqi: #ERROR

 

If I Hardcode the Candidate "John", "Sam"... it works.

Mease2:

Study Candidate Votes in Sharqi:=CALCULATE([Tt Votes],fVotesAmioun2016[CandidateEN]="John",fVotesAmioun2016[Region]="Sharqi")

 

Study Candidate Votes in Sharqi-644

 

Does Excel 2016 Pro Dax does not accept Variable Only HardCode? 

How to make the Candidate Dynamic As Vaiable, I change it in excel it flow throught all the rest of tables/Calculation

 

Later if you want what do you need to upload and how (Basic user) I upload to here Query and DataModel tables?

Thank you

D

6 Replies

  • Deku's avatar
    Deku
    Super User
    CALCULATE(
       [Tt Votes],
       fVotesAmioun2016[CandidateEN] in VALUES( CandidateCompared ) ,
       fVotesAmioun2016[Region]="Sharqi"
    )
    • DSR's avatar
      DSR
      Resolver I

      Your suggestion does not work. 

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi DSR,

        Thanks for reaching out to the Microsoft fabric community forum.

        It looks like you are trying to create a dynamic DAX measure that filters votes by region and a candidate name, where the candidate name is coming from an Excel cell. You're also using Power Pivot in Excel 2016. The logic of your measure (CALCULATE([Tt Votes], fVotesAmioun2016[CandidateEN]="John", fVotesAmioun2016[Region]="Sharqi")) is fine, the issue lies in how you're trying to pass the candidate name dynamically.

        The variable CandidateCompared you've created in Power Query returns a scalar value, just a single piece of text, like "John". DAX inside a measure doesn't accept that kind of external scalar reference directly. In Excel 2016 specifically, Power Pivot doesn't support passing variables from Power Query into DAX expressions in this way.

        You can use a disconnected table and SELECTEDVALUE. First create a small table in Excel (e.g., CandidateCompared_Criteria) with a single column listing candidate names then load it into the Data Model. Do not create a relationship between this table and others. Now add a slicer in your Excel sheet based on this table so you can select a candidate. Then try the this DAX measure "SelectedCandidate := SELECTEDVALUE(CandidateCompared_Criteria[CandidateCompared_Criteria])".

        Also rewrite your main measure:

        Study Candidate Votes in Sharqi :=
        CALCULATE(
        [Tt Votes],
        fVotesAmioun2016[CandidateEN] = [SelectedCandidate],
        fVotesAmioun2016[Region] = "Sharqi"
        )

        This approach works because DAX can evaluate [SelectedCandidate] as a valid scalar during measure evaluation.

         

        I would also take a moment to thank Deku, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.

         

        If I misunderstand your needs or you still have problems on it, please feel free to let us know.  

        Best Regards,
        Hammad.
        Community Support Team

         

        If this post helps then please mark it as a solution, so that other members find it more quickly.

        Thank you.