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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
ramchoudhary
Frequent Visitor

Flag Current Quarter's Rows By Comparing Previous Quarter's Set of Rows

Hi Team,

 

I need too flag a set of rows in current quarter based on the previous quarter's rows and name those rows with below mentioned conditons:

Below is the smaple data with desired output column named as "Result":

DateCompany IDSecurity IDQuarterResult
01-01-2021ABCD1110010121 
11-01-2021ABCD1210020121 
21-01-2021ABCD1110030121 
31-01-2021ABCD1210040121 
10-02-2021ABCD1410020221 
20-02-2021ABCD1110030221 
02-03-2021ABCD1210040321 
12-03-2021ABCD1110050321 
22-03-2021ABCD1210060321 
01-04-2021ABCD1310070421 
11-04-2021ABCD1310080421 
21-04-2021ABCD1210010421 
01-05-2021ABCD1610090521 
03-05-2021ABCD1610100521 
05-05-2021ABCD1310010521 
07-05-2021ABCD1510020521 
09-05-2021ABCD1210020521 
11-05-2021ABCD1110020521 
13-05-2021ABCD1010020521 
02-06-2021ABCD1010020621Current
12-06-2021ABCD1310180621New
21-06-2021ABCD1310190621New
23-06-2021ABCD1310010621Current
24-06-2021ABCD1210020621Current
25-06-2021ABCD1110020621Current
26-06-2021ABCD1010200621New
27-06-2021ABCD16 0621Exit
28-06-2021ABCD16 0621Exit
29-06-2021ABCD16 0621Exit

 

Current Selection in Quarter Slicer is 0621 - now, we are trying to achieve the OUTPUT column Result as per below 3 condtions :

  1. "New"      - Companies with Securities at selected quarter [Green rows] that didn't exist in Prior Quarter [Red rows]
  2. "Current" - Companies with Securities at selected quarter [Green rows] that exsits in Prior Quarter [Red rows]
  3. "Exit"       - Companies without Securities at selected quarter [Green rows] that had securities in Prior Quarter [Red rows]

Looking for a genuis to get this sorted.

 

Thanks,

Ram

1 ACCEPTED SOLUTION
v-kkf-msft
Community Support
Community Support

Hi @ramchoudhary ,

 

From your data, you should be comparing the current month with the previous month's data. So please try the following formula:

 

Measure = 
VAR tab =
    SUMMARIZE ( 'Table', 'Table'[Company ID], 'Table'[Security ID] )
VAR PriorMonth =
    FORMAT ( EDATE ( MAX ( 'Table'[Date] ), -1 ), "mmyy" )
VAR P_M_tab =
    CALCULATETABLE (
        SUMMARIZE ( 'Table', 'Table'[Company ID], 'Table'[Security ID] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Quarter] = PriorMonth )
    )
VAR Current_ =
    COUNTROWS ( INTERSECT ( tab, P_M_tab ) )
VAR Exit =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        'Table'[Security ID] = BLANK (),
        INTERSECT (
            VALUES ( 'Table'[Company ID] ),
            CALCULATETABLE (
                VALUES ( 'Table'[Company ID] ),
                FILTER ( ALL ( 'Table' ), 'Table'[Quarter] = PriorMonth )
            )
        )
    )
VAR New =
    COUNTROWS ( EXCEPT ( tab, P_M_tab ) )
RETURN
    SWITCH ( TRUE (), Current_ > 0, "Current", Exit > 0, "Exit", New > 0, "New" )

vkkfmsft_0-1650336279492.png


If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

You want to compare with the previous month (not with the previous quarter).  Am i correct?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-kkf-msft
Community Support
Community Support

Hi @ramchoudhary ,

 

From your data, you should be comparing the current month with the previous month's data. So please try the following formula:

 

Measure = 
VAR tab =
    SUMMARIZE ( 'Table', 'Table'[Company ID], 'Table'[Security ID] )
VAR PriorMonth =
    FORMAT ( EDATE ( MAX ( 'Table'[Date] ), -1 ), "mmyy" )
VAR P_M_tab =
    CALCULATETABLE (
        SUMMARIZE ( 'Table', 'Table'[Company ID], 'Table'[Security ID] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Quarter] = PriorMonth )
    )
VAR Current_ =
    COUNTROWS ( INTERSECT ( tab, P_M_tab ) )
VAR Exit =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        'Table'[Security ID] = BLANK (),
        INTERSECT (
            VALUES ( 'Table'[Company ID] ),
            CALCULATETABLE (
                VALUES ( 'Table'[Company ID] ),
                FILTER ( ALL ( 'Table' ), 'Table'[Quarter] = PriorMonth )
            )
        )
    )
VAR New =
    COUNTROWS ( EXCEPT ( tab, P_M_tab ) )
RETURN
    SWITCH ( TRUE (), Current_ > 0, "Current", Exit > 0, "Exit", New > 0, "New" )

vkkfmsft_0-1650336279492.png


If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.
Best Regards,
Winniz
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

HI, @ramchoudhary ,
Scratch my solution, this is absolutely awesome and will fill your needs.

@v-kkf-msft Thanks for this amazing solution.

ramchoudhary
Frequent Visitor

Hi vojtechsima,

Thank you so much for your help here. 

I would love to have .pbix file you wokred. I need to check few things to see if things are dynamic based on the quarter selection.

 

Thanks!

vojtechsima
Memorable Member
Memorable Member

Hi, @ramchoudhary 
There's probably a way better way, how to do it, however, I managed to solve it like this:

I created a new calculated Table for the Previous Quarter (to make my life easier):

vojtechsima_1-1650048852079.png

 

 

PreviousQuarter = 
var CurrentQuarter = MAX('Table'[Quarter])
var _previousQuarter = MAXX(FILTER('Table', 'Table'[Quarter] <> CurrentQuarter), 'Table'[Quarter])
return CALCULATETABLE(SELECTCOLUMNS('Table', "UniqueValue", 'Table'[UniqueValue], "Quarter", 'Table'[Quarter], "Company" , 'Table'[Company ID], "Security", 'Table'[Security ID], "IsBlank", IF(ISBLANK('Table'[Security ID])=TRUE(), TRUE(), FALSE())) , 'Table'[Quarter] = _previousQuarter)

 

 Then in the original Table, I created a new calculated column that takes Company and Security and put them together so I can get some Unique value:

 

UniqueValue = CONCATENATE('Table'[Company ID],'Table'[Security ID])

 

Then Finally I created this measure that creates the output, you wanted (Tho I am not sure about the Exit, please try it if it works for you):

vojtechsima_2-1650048961375.png

 

ResultNew = 

var currentCompany = 'Table'[Company ID]
var currentSecurityID = 'Table'[Security ID]
var Row_currentQuarter = 'Table'[Quarter]
var UniqueValue = 'Table'[UniqueValue]
var CoresspondingBlankCompany = MAXX(FILTER('PreviousQuarter', currentCompany = 'PreviousQuarter'[Company] && 'PreviousQuarter'[IsBlank] = True()),'PreviousQuarter'[Company])

var currentQuarter = MAX('Table'[Quarter])

var CurrentCheck = IF('Table'[Quarter] = currentQuarter && UniqueValue IN VALUES('PreviousQuarter'[UniqueValue]), "Current")
var NewCheck = IF('Table'[Quarter] = currentQuarter && NOT(UniqueValue IN VALUES('PreviousQuarter'[UniqueValue])), "New")
var ExitCheck = IF('Table'[Quarter] = currentQuarter && ISBLANK('Table'[Security ID])= TRUE() && currentCompany <> CoresspondingBlankCompany, "Exit")

var Result = SWITCH(FALSE(),
ISBLANK(CurrentCheck), "Current",
ISBLANK(ExitCheck), "Exit",
ISBLANK(NewCheck), "New")


return Result

 


I hope it does work for you.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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