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

Countrows of an Intersect result

Hi all,

 

I am trying to perform a left join (if null), like below:

 

image.png

 

Considering the following Table1:

 

DateID
2017-12-151
2017-12-152
2017-12-153
......
2018-01-151
2018-01-152
2018-01-153
2018-01-154
2018-01-155

 

What I want to know, is how much new IDs were added in 2018-01-15 compared to the previous month 2017-12-15 (should be 2, IDs 4 and 5 are presented in 2018-01 and were not in 2017-12).

 

To do so, I was first calculating previous month date, based on TODAY():

 

 

 

 

<p>"var __lastMonth = DATE(YEAR(TODAY()),MONTH(TODAY())-1,DAY(TODAY()))"</p>

 

 

 

 

Then, I filtered the table to get all ID related to __lastMonth:

 

 

 

 

<p>var __previous =SUMMARIZE(FILTER('Table1','Table1'[Date]=__lastMonth),'Table1'[ID])</p>

 

 

 

 

I also filtered the table for current date:

 

 

 

 

<p>var __current =SUMMARIZE(FILTER('Table1','Table1'[Date]=TODAY()),'Table1'[ID])</p>

 

 

 

 

and performed an intersect to get their differences:

 

 

 

 

<p>var __intersect = INTERSECT(__current,__previous)</p>

 

 

 

 

This should generate the following table:

ID
1
2
3

 

And the result that I was looking for was:

 

 

 

 

<p>RETURN COUNTROWS(__current) - COUNTROWS(__intersect)</p>

 

 

 

 

The problem: If I reproduce this logic creating a "Table", I see that __intersect is outputing correctly the table, however, when I try this code as a "Measure", the __previous came empty, and so does the __intersect. 

 

What is wrong? Is there a more efficient way to do this?

 

Thank you in advance.

1 ACCEPTED SOLUTION
EvertonRR
Frequent Visitor

The logic was working, there was a date filter in the page for current day, thats why it was not able to bring any value related to last month. Thank you for your replies.

View solution in original post

4 REPLIES 4
EvertonRR
Frequent Visitor

The logic was working, there was a date filter in the page for current day, thats why it was not able to bring any value related to last month. Thank you for your replies.

PaulDBrown
Community Champion
Community Champion

@EvertonRR 

Try:

 

 

New ids in Month = 
VAR currentId = VALUES(Table1[ID])
VAR PrevMonthID = CALCULATETABLE(VALUES(Table1[ID]), 
                    DATEADD(Table1[Date], -1, MONTH))
RETURN
COUNTROWS(EXCEPT(currentId, PrevMonthID))

 

 

To give you this:

New Id.JPG

 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






MFelix
Super User
Super User

Hi @EvertonRR ,

 

Try the following measure:

 

 

Measure =
VAR LAst_Date =
    DATEADD ( Dates[Date]; -1; MONTH )
VAR date_select =
    MAX ( Dates[Date] )
RETURN
    IF (
        MAXX (
            FILTER (
                ALL ( 'Join' );
                'Join'[Date] = LAst_Date
                    && 'Join'[ID] = MAX ( 'Join'[ID] )
            );
            'Join'[ID]
        ) > 0;
        1;
        0
    )

 

 

Then filter your visual by the 1 or 0.

 

I'm assuming that you don't have ID that dissapear from one date to the other.

 

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



nhoward
Resolver I
Resolver I

@EvertonRR 

 

I assume you are working within PowerBI desktop.  You might need to change the way you think about data; as PBI is not SQL, it does not do joins.  The relationships are links that allow for filtering.  One table filters the other table, reducing the number of rows that it has.  You then perform a calculation on one of those columns.   To complicate things further, when you put a measure into a visual, you are adding further filters (each row and column of a matrix is a filter). 

 

Aussming that the ID column is always auto-incrementing by 1, then your measure could be based on the Difference between the MAX ID this month and the MAX ID last month.  You don't mention that you have a master date table, so I'll assume you don't have one for this formula. 

 

New Items this month = MAX(ID) - CALCULATE(MAX(ID),DATEADD(Date,-1,Month))

 

You then put this measure in a table or matrix with the month as a column or row. 

MAX(ID) will be the month of the row.  The Calculate uses the month of the row to obtain last month, and filters the table for that. 

 

Hope that helps, 

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.