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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
bizintelguy604
Frequent Visitor

Measure with multiple if statements and filters

I'm trying to create a new measure with multiple if statements however I'm getting an error. Any advice?

 

If ListingStatus = "Inactive" then calculate a particular expression using Calculate(sumx( with filters.

If ListingStatus = "Active" then calculate the same express with slightly different sumx filters.

 

Here is the measure:
*************************************************
Avg Asking Rate (Weighted) :=
IF (
      Listing[ListingStatus] = "Inactive",
      DIVIDE (
             CALCULATE (
                   SUMX ( 'Listing', Listing[NetAskRate] * Listing[ListingAvailableSF] ),
                   Listing[NetAskRate] > 0,
                   DATESINPERIOD (
                         ListingRemovalDate[ListingRemovalDate_DateOnly],
                         TODAY (),
                          -12,
                          MONTH
                    )
               ),
               CALCULATE (
                     SUM ( [ListingAvailableSF] ),
                     Listing[NetAskRate] > 0,
                     DATESINPERIOD (
                             ListingRemovalDate[ListingRemovalDate_DateOnly],
                             TODAY (),
                              -12,
                           MONTH
                )
         )
),
IF (
        Listing[ListingStatus] = "Active",
        DIVIDE (
              CALCULATE (
                      SUMX ( 'Listing', Listing[NetAskRate] * Listing[ListingAvailableSF] ),
                      Listing[NetAskRate] > 0
               ),
               CALCULATE ( SUM ( [ListingAvailableSF] ), Listing[NetAskRate] > 0 )
          )
     )
)


 

 

**************************************************
Here is the Error Warning:

Warning 1 Measure 'Listing'[Avg Asking Rate (Weighted)] : The value for column 'ListingStatus' in table 'Listing' cannot be determined in the current context. Check that all columns referenced in the calculation expression exist, and that there are no circular dependencies. This can also occur when the formula for a measure refers directly to a column without performing any aggregation--such as sum, average, or count--on that column. The column does not have a single value; it has many values, one for each row of the table, and no row has been specified. P:\MarketingTabularModel\MarketingTabularModel\Model.bim 0 0 MarketingTabularModel

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Hi @bizintelguy604 ,

the first argument in your If-statement is not valid:

 

Avg Asking Rate (Weighted) :=
IF (
      Listing[ListingStatus] = "Inactive",

 

this is referencing a whole column, but you need a single value (scalar) instead.
This could be achieved  by an aggregtion function, like MIN or MAX:

 

Avg Asking Rate (Weighted) :=
IF (
      MAX ( Listing[ListingStatus] ) = "Inactive",

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

4 REPLIES 4
amitchandak
Super User
Super User

@bizintelguy604 , Try like

Avg Asking Rate (Weighted) :=
IF (
      max(Listing[ListingStatus]) = "Inactive",
      DIVIDE (
             CALCULATE (
                   SUMX ( 'Listing', Listing[NetAskRate] * Listing[ListingAvailableSF] ),
                   Listing[NetAskRate] > 0,
                   DATESINPERIOD (
                         ListingRemovalDate[ListingRemovalDate_DateOnly],
                         TODAY (),
                          -12,
                          MONTH
                    )
               ),
               CALCULATE (
                     SUM ( [ListingAvailableSF] ),
                     Listing[NetAskRate] > 0,
                     DATESINPERIOD (
                             ListingRemovalDate[ListingRemovalDate_DateOnly],
                             TODAY (),
                              -12,
                           MONTH
                )
         )
),
IF (
        max(Listing[ListingStatus]) = "Active",
        DIVIDE (
              CALCULATE (
                      SUMX ( 'Listing', Listing[NetAskRate] * Listing[ListingAvailableSF] ),
                      Listing[NetAskRate] > 0
               ),
               CALCULATE ( SUM ( [ListingAvailableSF] ), Listing[NetAskRate] > 0 )
          )
     )
)

Hi @amitchandak ,

wondering if you spotted other flaws in the measure or did you just write out the full measure code with the changes I suggested?

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

@ImkeF , My bad miss. Thanks for pointing. 

ImkeF
Super User
Super User

Hi @bizintelguy604 ,

the first argument in your If-statement is not valid:

 

Avg Asking Rate (Weighted) :=
IF (
      Listing[ListingStatus] = "Inactive",

 

this is referencing a whole column, but you need a single value (scalar) instead.
This could be achieved  by an aggregtion function, like MIN or MAX:

 

Avg Asking Rate (Weighted) :=
IF (
      MAX ( Listing[ListingStatus] ) = "Inactive",

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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