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
Lily780
Frequent Visitor

Dax Measure return countrows with case statement (divide, substract)

Hi Experts,

 

I have a truble with counting of rows in a measure. I have a table 

 

Lily780_0-1671393396129.png

And this table has a relationship to Calendar table (Table.Order Date = Calendar.Date)

Lily780_1-1671393462460.png

I have a table visualization with all columns except Inerval and I need to count rows with specific conditions.

Lily780_2-1671393537345.png

CASE WHEN ( ABS(TO_NUMBER(Calendar [Year])-
( CASE WHEN TO_NUMBER(TO_CHAR(Table.Order Date,'MM')) < 4
THEN TO_NUMBER(TO_CHAR(Table.Order Date,'YYYY'))-1
ELSE TO_NUMBER(TO_CHAR(Table.Order Date,'YYYY')) END )) ) = 0
THEN 1
WHEN Table."Interval" > 0 AND MOD(( ABS(TO_NUMBER(:Calendar [Year])-
( CASE WHEN TO_NUMBER(TO_CHAR(Table.Order Date,'MM')) < 4
THEN TO_NUMBER(TO_CHAR(Table.Order Date,'YYYY'))-1
ELSE TO_NUMBER(TO_CHAR(Table.Order Date,'YYYY')) END )) ),
Table."Interval" )) = 0 THEN 1 ELSE 0 END

Could you help me with this calculation please?

2 REPLIES 2
Lily780
Frequent Visitor

Hey @v-yiruan-msft ,

Thanks a lot for your answer and help!

But this is not the full logic of what I want to show. I have attached the file (link https://drive.google.com/drive/folders/1ntXXfaaK09z8GCHOOoyORNkfTcRwoEBa?usp=sharing ). I changed the data set a bit and you can see 2 columns in the table. 2023 and 2021.

These columns explain what I want to get.
These columns must be generated for each year from Order Year. I have about 30 years in my data and therefore it will be tedious to calculate each column in Power Query. At the same time, the dynamic calculation in Power Bi is impossible. The slicer should depend on the number of years in the Date Order

Thefore it is not enough for me to sum up total values (like calculate) based on the Interval to display a unique number of rows. Perhaps I need an iterative measure that can display the unique number of the string, and not the sum of the interval.

For example, for 2023, I should get not 6 rows with 1, but 15 rows and 11 of them with 1 (in Flag column).

 

Lily780_4-1671705361763.png

For example, ID 187 has 2 values in table and two of them = 1,

Lily780_1-1671705024161.png

So, I need to return 1 rows with Inetrval = 2 (sum of rows) and Flag =1

Lily780_2-1671705069200.png

But for ID 18789006 and Order Date 04/07/2021 instaed of 3 rows I need to display 2:
Lily780_6-1671705493744.png


Hopefully, my explanation is clear

Thanks

v-yiruan-msft
Community Support
Community Support

Hi @Lily780 ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want. You can create a measure as below:

 

Flag = 
VAR _selyear =
    SELECTEDVALUE ( 'Calendar'[Year] )
VAR _ordate =
    SELECTEDVALUE ( 'Table'[Order Date] )
VAR _interval =
    SELECTEDVALUE ( 'Table'[Interval] )
VAR _ordyear =
    YEAR ( _ordate )
VAR _ordmonth =
    MONTH ( _ordate )
VAR _diff =
    ABS ( _selyear - IF ( _ordmonth < 4, _ordyear - 1, _ordyear ) )
RETURN
    IF (
        _diff = 0
            || (
                _interval > 0
                    && IF ( _interval <> 0, MOD ( _diff, _interval ) ) = 0
            ),
        1,
        0
    )

 

yingyinr_0-1671433046658.png

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And it is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community.

How to upload PBI in Community

Best Regards

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

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.