Forum Discussion

Gazi_Sohan's avatar
Gazi_Sohan
Helper I
2 years ago
Solved

Need Help with Calculate

Hi guys, I am trying to calculate Net Income between 2 accounts (account numbers between 400000 and 957090), however I want the account number to come from the columns instead of hard coded. I tried it and got the following exception.
How can I fix this ?

 

 

Here is the DAX measure that I am currently using

 

Net_income =
CALCULATE(
[Actual Amounts],
// COADetails_New[AccountID] >= "400000" && COADetails_New[AccountID] <= "957090"
COADetails_New[AccountID] >= 'Balance Sheet Table'[Start Account] && COADetails_New[AccountID] <= 'Balance Sheet Table'[End Account]
)

 

 

  • Hey Anonymous 
    Thanks for your solution. Though it's working on the sample file but it didn't work out in our main file.

    I have tried my own way by using a pretty simple approach i.e. using selectedvalue function and it's working perfect in the measure and made it dynamic - 



    I have applied the same concept in the sample POC file that I provided for you (attached) - 


    and it's also working fine in the sample file - 

    But anyway, thanks for your help. I really appreciate it. You are a very talneted guy I must say.
    Hats off ðŸ’¥


    .pbix file with my dynamic measure - 
    https://drive.google.com/file/d/1jRhL9q_ytVP4OxSyjntFliLqdm_IXynP/view?usp=sharing

12 Replies

  • tamerj1's avatar
    tamerj1
    Community Champion

    Hi Gazi_Sohan 

    please try

    Net_income =
    CALCULATE (
    [Actual Amounts],
    COADetails_New[AccountID]
    >= SELECTEDVALUE ( 'Balance Sheet Table'[Start Account] ) && COADetails_New[AccountID]
    <= SELECTEDVALUE ( 'Balance Sheet Table'[End Account] )
    )

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Gazi_Sohan ,

     

    Could you tell me if your problem has been solved? If it is, kindly Accept it as the solution. More people will benefit from it. Or if you are still confused about it, please feel free to let me know.

     

    Best Regards,

    Neeko Tang

    If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

    • Gazi_Sohan's avatar
      Gazi_Sohan
      Helper I

      Hi Anonymous 
      Sorry for the late reply as I was too busy with loads of projects in my company.

       

      There are some corrections that the company has made. So now, instead of the Balance Sheet table, we are using COADetails_New table which is connected to the Fact table directly with a "One to Many" relationship as shown below in the data model - 


      Also, we have got the DAX measure working but still it has got a hard-coded portion in it in as a filter
      // COADetails_New[AccountID] >= "400000" && COADetails_New[AccountID] <= "957090")

      See the screenshot below - 
       

      We don't want this portion to be hard-coded.
      Instead we want to read it from the table itself i.e. COADetails_New table.

      In the COADetails_New table, here are a snap of the main columns that can be taken into account to perform the required operation - 

      In our current DAX measure we have hard-coded the AccountID for a range between 400000 and 957090 (as mentioned in my first post). Also we don't have the columns Start Account and End Account in COADetails_New table like we had before in the Balance Sheet table (I mentioned in my first post).

      Now, how can I put the range dynamically instead of hard-coding ? Remember, you can't use the totalling column from COADetails_New table and break it into Start and End account because it remains outdated, the client never updates this.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Gazi_Sohan ,

         

        How would you like to dynamically display the ID?
        Is 400000 the minimum value of AccountID column and 957090 the maximum value of AccountID column?
        Then you can use the

        var _minid= MINX(ALL('COADetails_New'),[AccountID])
        VAR _maxid= MAXX(ALL('COADetails_New'),[AccountID])
        ...
        COADetails_New[AccountID] >= __minid && COADetails_New[AccountID] <= _maxid
        ...

        Or you can create a slicer table. This allows you to manually select the id interval you want.

        SlicerTable = VALUES(COADetails_New[AccountID])
        
        var _minid= MINX(ALLSELECTED('SlicerTable'),[AccountID])
        VAR _maxid= MAXX(ALLSELECTED('SlicerTable'),[AccountID])
        ...
        COADetails_New[AccountID] >= __minid && COADetails_New[AccountID] <= _maxid
        ...

        Best Regards,

        Neeko Tang

        If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

  • Hi Anonymous 
    Let me simplify it for you. Forget about Net Income.

    I have a created the DAX measures in which there is a filtering on Account ID column of COADetails table. 

    Now the issue is that, in the measure, when I write the Account ID numbers as hardcoded, the measure works, but when I write those dynamically, it doesn't work. To avoid confusion, I have created the same DAX measure twice, one being hardcoded and the other being dynamic - 

    I am providing the necessary files i.e. the datasource (excel file) and the Power BI report which you will get in the below link - 
    https://drive.google.com/drive/folders/1xDiFgNOm_OpwcqfcgNhInF1homob_suP?usp=sharing


    Can you please tell me what am I doing wrong ?

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Gazi_Sohan ,

       

      We can create two calculated columns on table COA Details.

      Column 1 = MID([Operator Between Totaling], 1, SEARCH("..", [Operator Between Totaling], , 1) - 1) 
      Column 2 = MID([Operator Between Totaling], SEARCH("..", [Operator Between Totaling], , 1) + 2, LEN([Operator Between Totaling]))

      Then we can update the measure.

      Dynamic Accounts = 
      var selectedrecord = SELECTEDVALUE('COA Summary'[Account ID])
      var _Description=SELECTEDVALUE('COA Summary'[Account Description])
      var _a=CALCULATE(MAX('COA Details'[Column 1]),FILTER(ALL('COA Details'),'COA Details'[Account Description]=_Description))
      var _b=CALCULATE(MAX('COA Details'[Column 2]),FILTER(ALL('COA Details'),'COA Details'[Account Description]=_Description))
      var _c=CALCULATE(MAX('COA Details'[Column 1]),FILTER(ALL('COA Details'),'COA Details'[Account Description]=_Description))
      var _d=CALCULATE(MAX('COA Details'[Column 2]),FILTER(ALL('COA Details'),'COA Details'[Account Description]=_Description))
      RETURN
      SWITCH(
          selectedrecord,
          106,
          CALCULATE(
              SUM(Transactions[Amount]),
              ALL('COA Details'),
              'COA Details'[Account ID] >= CONVERT(_a,INTEGER)
              && 'COA Details'[Account ID] <= CONVERT(_b,INTEGER)
          ),
          206,
          CALCULATE(
              SUM(Transactions[Amount]),
              ALL('COA Details'),
              'COA Details'[Account ID] >= CONVERT(_c,INTEGER)
              && 'COA Details'[Account ID] <= CONVERT(_d,INTEGER)
          ),
          CALCULATE(
              SUM(Transactions[Amount]),
              'COA Details'[Account ID] >= 'COA Details'[Operator Between Start]
              && 'COA Details'[Account ID] <= 'COA Details'[Operator Between End]
          )
      )

      Best Regards,

      Neeko Tang

      If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

      • Gazi_Sohan's avatar
        Gazi_Sohan
        Helper I

        Hey Anonymous 
        Thanks for your solution. Though it's working on the sample file but it didn't work out in our main file.

        I have tried my own way by using a pretty simple approach i.e. using selectedvalue function and it's working perfect in the measure and made it dynamic - 



        I have applied the same concept in the sample POC file that I provided for you (attached) - 


        and it's also working fine in the sample file - 

        But anyway, thanks for your help. I really appreciate it. You are a very talneted guy I must say.
        Hats off ðŸ’¥


        .pbix file with my dynamic measure - 
        https://drive.google.com/file/d/1jRhL9q_ytVP4OxSyjntFliLqdm_IXynP/view?usp=sharing