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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
yusufashiq
Frequent Visitor

Filter a table on selected date value and find max date field records

I have a Table

yusufashiq_3-1644737756831.png

 

I need the result table similar to use in SQL Server, the script is 

 SELECT CAST(t.EmpNo AS int) AS EMpNum, t.ProjTo AS ToDept, t.TransferDate, t.TrType
FROM dbo.Transfer AS t RIGHT OUTER JOIN
(SELECT CAST(EmpNo AS int) AS EMpNum1, MAX(TransferDate) AS MaxDate
FROM dbo.Transfer
WHERE (TransferDate <= '2022-01-31') and EmpNo=71103
GROUP BY CAST(EmpNo AS int)) AS tm ON CAST(t.EmpNo AS int) = CAST(tm.EMpNum1 AS int) AND t.TransferDate = tm.MaxDate
WHERE (t.TrType = 1)
If my Selected Value (is Payroll Period='2022-01-31') then

 the result is

yusufashiq_4-1644737807762.png

 

where I need to use the TransferDate condition like a variable to filter based on selected date value using DAX.
If my Selected Value (is Payroll Period='2022-02-28') then

the result is 

yusufashiq_6-1644737853997.png

 

How to achieve this table result same as SQL Server in Power BI Transform Data

Note: The selected value is a variable from the Visual selection

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

Hi  @yusufashiq ,

Here are the steps you can follow:

1. Create calculated table.

Date =
CALENDAR(DATE(2021,1,1),DATE(2022,12,31))

vyangliumsft_0-1644977058797.png

2. Create measure.

Flag =
var _select=SELECTEDVALUE('Date'[Payroll Period])
var _if=CALCULATE(MAX('Table'[TransferDate]),FILTER(ALL('Table'),'Table'[TrType]=1&&'Table'[EmpNo]=71103&&'Table'[TransferDate]<=_select))
return
IF(
    MAX('Table'[TrType])=1&& MAX('Table'[EmpNo])=71103&&MAX('Table'[TransferDate])=_if,1,0)

3. Place [Flag]in Filters, set is=1, apply filter.

vyangliumsft_1-1644977058799.png

4. Result:

Take the [Payroll Period] of the Date table as the slicer.

When the selection is: 2022.1.31

vyangliumsft_2-1644977058802.png

When selected as: 2022.2.28

vyangliumsft_3-1644977058803.png

 

Best Regards,

Liu Yang

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

1 REPLY 1
v-yangliu-msft
Community Support
Community Support

Hi  @yusufashiq ,

Here are the steps you can follow:

1. Create calculated table.

Date =
CALENDAR(DATE(2021,1,1),DATE(2022,12,31))

vyangliumsft_0-1644977058797.png

2. Create measure.

Flag =
var _select=SELECTEDVALUE('Date'[Payroll Period])
var _if=CALCULATE(MAX('Table'[TransferDate]),FILTER(ALL('Table'),'Table'[TrType]=1&&'Table'[EmpNo]=71103&&'Table'[TransferDate]<=_select))
return
IF(
    MAX('Table'[TrType])=1&& MAX('Table'[EmpNo])=71103&&MAX('Table'[TransferDate])=_if,1,0)

3. Place [Flag]in Filters, set is=1, apply filter.

vyangliumsft_1-1644977058799.png

4. Result:

Take the [Payroll Period] of the Date table as the slicer.

When the selection is: 2022.1.31

vyangliumsft_2-1644977058802.png

When selected as: 2022.2.28

vyangliumsft_3-1644977058803.png

 

Best Regards,

Liu Yang

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

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.