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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
VedranR
Frequent Visitor

Need help with metric in direct query mode

Hi everyone,

I need help with some issue in PBI.

In Power BI I have two tables 'Calendar' and 'report dealer_count_event_view'.
In the 'Calendar' table I have the data Date, while in the 'report dealer_count_event_view' table (which I import via direct query) we record all status changes (event_type) by users and I have separate data only for the date (event_date) and for the date and time together (event_time) for each record.


It can happen that a user has multiple records on the same day, but with different times.


I need to create a report that will count only users whose last record in the observed month is deactivated (which does not have to be on the last day of the month), but that in the same month, on the day before that last record with the status (event_type) deactivated, there is a record with a status different from deactivated.

 

I have a metric that works when the 'report dealer_count_event_view' table is not in direct query mode (ther is error fatching problem), but works when data is imported.


Is it possible to implement this in a single DAX metric in data query mode?

 

This is what the 'report dealer_count_event_view' dataset looks like:

 

user_idevent_timeevent_dateevent_type
82325.12.2024 08:4125.dec.24deactivated
82322.1.2025 16:1122.jan.25reactivated
82323.1.2025 17:2523.jan.25deactivated
82319.2.2025 15:5119.feb.25reactivated
1752516.2.2025 16:3516.feb.25reactivated
1967020.1.2025 19:2120.jan.25deactivated
19250716.12.2024 13:2016.dec.24deactivated
19250713.1.2025 15:1113.jan.25reactivated
19250715.1.2025 15:1215.jan.25deactivated
53511417.11.2024 09:1217.nov.24deactivated
53511417.11.2024 08:3617.nov.24reactivated
53511421.2.2025 19:3321.feb.25reactivated
53511422.2.2025 19:2522.feb.25deactivated
67619221.2.2025 15:4121.feb.25deactivated
67619224.2.2025 11:2524.feb.25reactivated

 

Thanks in advance!

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

Hi,

Thanks for the solution @mark_endicott  offered, and i want to offer some more information for user to refer to.

hello @VedranR , you can refer to the following solution

The sample data is the same as you provided, create the following measures.

MEASURE =
VAR a =
    FILTER (
        ALLSELECTED ( 'report dealer_count_event_view' ),
        [user_id] IN VALUES ( 'report dealer_count_event_view'[user_id] )
    )
VAR b =
    MAXX ( a, [event_time] )
RETURN
    MAXX ( FILTER ( a, [event_time] = b ), [event_type] )
Measure2 =
VAR a =
    FILTER (
        ALLSELECTED ( 'report dealer_count_event_view' ),
        [user_id] IN VALUES ( 'report dealer_count_event_view'[user_id] )
    )
VAR b =
    MAXX ( a, [event_time] )
RETURN
    MAXX (
        TOPN ( 1, FILTER ( a, [event_time] < b ), [event_time], DESC ),
        [event_type]
    )
Measure3 =
CALCULATE (
    DISTINCTCOUNT ( 'report dealer_count_event_view'[user_id] ),
    FILTER (
        'report dealer_count_event_view',
        [Measure 2] <> "deactivated"
            && [Measure] = "deactivated"
    )
)

OUTPUT

vxinruzhumsft_0-1740710726826.png

Best Regards!

Yolo Zhu

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

2 REPLIES 2
v-xinruzhu-msft
Community Support
Community Support

Hi,

Thanks for the solution @mark_endicott  offered, and i want to offer some more information for user to refer to.

hello @VedranR , you can refer to the following solution

The sample data is the same as you provided, create the following measures.

MEASURE =
VAR a =
    FILTER (
        ALLSELECTED ( 'report dealer_count_event_view' ),
        [user_id] IN VALUES ( 'report dealer_count_event_view'[user_id] )
    )
VAR b =
    MAXX ( a, [event_time] )
RETURN
    MAXX ( FILTER ( a, [event_time] = b ), [event_type] )
Measure2 =
VAR a =
    FILTER (
        ALLSELECTED ( 'report dealer_count_event_view' ),
        [user_id] IN VALUES ( 'report dealer_count_event_view'[user_id] )
    )
VAR b =
    MAXX ( a, [event_time] )
RETURN
    MAXX (
        TOPN ( 1, FILTER ( a, [event_time] < b ), [event_time], DESC ),
        [event_type]
    )
Measure3 =
CALCULATE (
    DISTINCTCOUNT ( 'report dealer_count_event_view'[user_id] ),
    FILTER (
        'report dealer_count_event_view',
        [Measure 2] <> "deactivated"
            && [Measure] = "deactivated"
    )
)

OUTPUT

vxinruzhumsft_0-1740710726826.png

Best Regards!

Yolo Zhu

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

 

 

mark_endicott
Super User
Super User

@VedranR - If your DAX works in Import mode, then you should be asking the question why this needs to be Direct Query. 

 

Performance will always be worse, and whilst end users say they need 'live' data, but are they really sat there watching your report refresh 24/7 - unlikely. 

 

If you would like someone to help, you'll need to supply your DAX. 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

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