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
pandapanda
Regular Visitor

SQL to DAX - iterative table query

I have SQL like this

 

select count(*) from myTABLE f1 join myTABLE f2
on f1.customerid = f2.customerid
where f1.reportingdate='2017-10-31' and f2.reportingdate='2017-10-30'
and f1.currentstatus='1' and f2.currentstatus <>'1'

 

I want to get this as a measure in PowerBI desktop.

The dates should be dynamic from a date heirachy slider (table already created)

 

How can i get this is DAX

 

 

1 ACCEPTED SOLUTION

@pandapanda,

 

It's seems someone provide a solution for your requirement on your another thread, pelase check it.
http://community.powerbi.com/t5/Desktop/Report-number-of-changes-in-a-field/m-p/310698#M137616

 

number of users that change to currentstatus regular =
VAR summizedTbl =
    SUMMARIZE (
        yourTable,
        yourTable[customerid],
        "Distinct Status count", DISTINCTCOUNT ( yourTable[currentstatus] ),
        "status", CONCATENATEX (
            yourTable,
            yourTable[currentstatus],
            "/",
            yourTable[reportingdate], DESC
        )
    )
VAR rowCnt =
    COUNTROWS (
        FILTER (
            summizedTbl,
            [Distinct Status count] > 1
                && LEFT ( [status], 7 ) = "Regular"
        )
    )
RETURN
    IF ( ISBLANK ( rowCnt ), 0, rowCnt )

 

Regards,

Charlie Liao 

View solution in original post

11 REPLIES 11
parry2k
Super User
Super User

@pandapanda 

 

 

Can you share sample data for both the tables?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Its only 1 table!! i am using it against itself to work out differences

 

The table is like this

 

ID       customerID   Date                Status

1             1212          2/3/2016         1

2             1213          2/3/2016         2

3             1212          2/4/2016         4

4             1213          2/4/2016         4

 

For every day, you get row for each customer

over time the status changes

 

What I want to know is the changes to and from status. 

so i want on PowerBI a number where the number of changes to status X happens over a period of time

 

 

i guess in this sample your output will be

 

Status   #of changes

1                 1

2                 1

4                 2

 

correct?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Yes that is correct, but what I want in the report is to show how many users have changed to status (dynamic/slicer to choose) over a certain time perid.

 

in the data i give, 

If I were to select the last 60 days and want to know how many users moved to status 2 then the answer I want is 2!

 

 

based on your sample date , answer will be 1 the scenario you explained below, no?

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Your previous post: 

Status   #of changes

1                 1

2                 1

4                 2

 

correct?

----------------

 

Those are the right numbers - i want to get that last result -  #changes is 2 where status is 4 for that time period!

 

 

@pandapanda in table visual add status, and then add status again and change the aggregation of 2nd status to distinct count

 

add date slicer and your table visual will filter based on the date range you select in date slicer



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

For the three use cases

1.start is 8th nov and end date is 12th November  there are 2 two customers (id 1 and 2) that change to Regular hence  the answer is 2

2. 8th nov and end date is 10th November no one changes to regular hence 0 (in fact here id3 gooes to sleeper)

3. Expected result if date start is 11th nov and end date is 12th November is 2 NOT 1 as per the sample sheet answer!! as id1 and 3 go to regular

 

Sorry!!

but i am sure the main data set will be easier to get some numbers

@pandapanda,

 

It's seems someone provide a solution for your requirement on your another thread, pelase check it.
http://community.powerbi.com/t5/Desktop/Report-number-of-changes-in-a-field/m-p/310698#M137616

 

number of users that change to currentstatus regular =
VAR summizedTbl =
    SUMMARIZE (
        yourTable,
        yourTable[customerid],
        "Distinct Status count", DISTINCTCOUNT ( yourTable[currentstatus] ),
        "status", CONCATENATEX (
            yourTable,
            yourTable[currentstatus],
            "/",
            yourTable[reportingdate], DESC
        )
    )
VAR rowCnt =
    COUNTROWS (
        FILTER (
            summizedTbl,
            [Distinct Status count] > 1
                && LEFT ( [status], 7 ) = "Regular"
        )
    )
RETURN
    IF ( ISBLANK ( rowCnt ), 0, rowCnt )

 

Regards,

Charlie Liao 

I have tried  - I add time slicer based on a time table and use it but it gives me really low number ( am expecting 00's not 1 or 2)

 

help1.jpg

The two users  1212  and 1213  both change to status 4  (from status 1 and 2 ) in the time period (60days) 

 

Is that clear?

 

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.