Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.
Hi,
Fairly new to Power BI and struggle with a DAX query.
Suppose I have the following model:
The FACT_PAYMENTS table contain payment transactions with a due date and customer link.
I want to add a new measure, which for any record, summarizes the payment amount which is 'passed due'. This would be a sum of all payment amounts [FACT_PAYMENT.AMOUNT] where the due date [DIM_DATE.GREGORIANDATE] is less than due for the current record and the customer ID [FACT_PAYMENT.CustRecId] is the same.
I can do this in SQL by writing a subquery with a self-join, but I would like to avoid doing such calculations in the warehouse layer.
Any help with this would be much appreciated.
Thanks in advance,
Nick
Solved! Go to Solution.
Hi @Gooner_chef ,
Sorry for delay. I created a sample pbix file(see attachment) for you, please check whether that is what you want.
1. Create a measure as below to get the sum of amount which pass due date
AmountPassedDue =
VAR _curcust =
SELECTEDVALUE ( 'FACT_PAYMENT'[CustRecId] )
VAR _duedate =
SELECTEDVALUE ( 'DIM_DATE'[GREGORIANDATE] )
RETURN
CALCULATE (
SUM ( 'FACT_PAYMENT'[Amount] ),
FILTER ( ALLSELECTED ( 'FACT_PAYMENT' ), 'FACT_PAYMENT'[CustRecId] = _curcust ),
FILTER ( ALLSELECTED ( 'DIM_DATE' ), 'DIM_DATE'[GREGORIANDATE] < _duedate )
)
2. Create a table visual apply the measure on it
Best Regards
Hi @Gooner_chef ,
On which two fields are the tables DIM_DATE and FACT_PAYMENT created relationships based on respectively? Could you please provide some sample data for these two tables (excluding sensitive data)? You can create a measure as below to get the sum of amount under the special conditions...
Measure =
CALCULATE (
SUM ( FACT_PAYMENT[AMOUNT] ),
FILTER (
FACT_PAYMENT,
FACT_PAYMENT[xxdate] > RELATED ( DIM_DATE[GREGORIANDATE] )
&& FACT_PAYMENT[CustRecId] = SELECTEDVALUE ( FACT_PAYMENT[CustRecId] )
)
)
Best Regards
Hi yingyinr,
Thank you so much for the reply.
This looks close. The relationship is [FACT_PAYMENT].[DueDateRecId] = [DIM_DATE].[DateRecId].
The actual date value is stored in [DIM_DATE].[GregorianDate]. So I would first need to join the two tables, and then do the date comparison.
Hopes this makes sense!
Hi @Gooner_chef ,
According to the screenshot in your first post, you have created two relationships between table DIM_DATE and FACT_PAYMENT (one is active, the other one is inactive). This relationship ([FACT_PAYMENT]. [DueDateRecId] = [DIM_DATE]. [DateRecId]) is active or inactive? In addition to this relationship, which two columns in each of these two tables create the relationship based on? Could you please share some sample data from these two tables(exclude sensitive data) and the SQL statements where you used to achieve the same function in order to provide you with a suitable solution? Thank you.
Best Regards
Hi @Anonymous
Here is the SQL I used to achieve a similar result:
SELECT
ct1.ACCOUNTNUM,
dim1.DateRecID,
SUM([AMOUNTCUR]) as AmountDue,
(
SELECT
SUM(AMOUNTCUR)
FROM CustTrans ct2
INNER JOIN common.dim_date dim2 ON ct2.[DUEDATE] = dim2.GREGORIANDATE
WHERE (ct1.ACCOUNTNUM = ct2.ACCOUNTNUM)
AND (ct2.DUEDATE < ct1.DUEDATE)
) As AmountPassedDue
FROM CUSTTRANS ct1
INNER JOIN common.dim_date dim1 ON ct1.[DUEDATE] = dim1.GREGORIANDATE
GROUP BY ACCOUNTNUM, dim1.DateRecID, ct1.DUEDATE
The tables name are slightly different, but the principle is the same.
Yes, there are mutliple relations to the DIM_DATE table (DueDateRecId and TransDateRecId). DueDateRecId will be the active relation.
@Anonymous
Here is some example data:
FACT_PAYMENT:
DIM_DATE:
Hi @Gooner_chef ,
Sorry for delay. I created a sample pbix file(see attachment) for you, please check whether that is what you want.
1. Create a measure as below to get the sum of amount which pass due date
AmountPassedDue =
VAR _curcust =
SELECTEDVALUE ( 'FACT_PAYMENT'[CustRecId] )
VAR _duedate =
SELECTEDVALUE ( 'DIM_DATE'[GREGORIANDATE] )
RETURN
CALCULATE (
SUM ( 'FACT_PAYMENT'[Amount] ),
FILTER ( ALLSELECTED ( 'FACT_PAYMENT' ), 'FACT_PAYMENT'[CustRecId] = _curcust ),
FILTER ( ALLSELECTED ( 'DIM_DATE' ), 'DIM_DATE'[GREGORIANDATE] < _duedate )
)
2. Create a table visual apply the measure on it
Best Regards
Wow! This is really cool. Using variables make this very easy to understand! Thank you so much for your help 🙂
Hi @Anonymous
Yes, you are correct. DIM_DATE is a role-playing dimension in this instance. The FACT_PAYMENT contains two relations to DIM_DATE; TransDateRecid and DueDateRecId. For simplicity, I can ensure that the DueDate relation is active.
Here is example SQL I used to achieve the result I need:
SELECT
ct1.ACCOUNTNUM,
dim1.DateRecID,
SUM([AMOUNTCUR]) as AmountDue,
(
SELECT
SUM(AMOUNTCUR)
FROM CustTrans ct2
INNER JOIN common.dim_date dim2 ON ct2.[DUEDATE] = dim2.GREGORIANDATE
WHERE (ct1.ACCOUNTNUM = ct2.ACCOUNTNUM)
AND (ct2.DUEDATE < ct1.DUEDATE)
) As AmountPassedDue
FROM CUSTTRANS ct1
INNER JOIN common.dim_date dim1 ON ct1.[DUEDATE] = dim1.GREGORIANDATE
GROUP BY ACCOUNTNUM, dim1.DateRecID, ct1.DUEDATE
The tables/column have a slighlty different name, but the principle is the same.
Here is some example data.
DIM_DATE:
DateRecID | GREGORIANDATE |
Dec 29 2019 12:00AM-0-FiscalCalendar | 12/29/2019 |
Dec 29 2019 12:00AM-1-FiscalCalendar | 12/29/2019 |
Dec 30 2019 12:00AM-1-FiscalCalendar | 12/30/2019 |
Dec 31 2019 12:00AM-1-FiscalCalendar | 12/31/2019 |
Jan 1 2020 12:00AM-1-FiscalCalendar | 1/1/2020 |
Jan 2 2020 12:00AM-1-FiscalCalendar | 1/2/2020 |
Jan 3 2020 12:00AM-1-FiscalCalendar | 1/3/2020 |
Jan 4 2020 12:00AM-1-FiscalCalendar | 1/4/2020 |
Jan 5 2020 12:00AM-1-FiscalCalendar | 1/5/2020 |
Jan 6 2020 12:00AM-1-FiscalCalendar | 1/6/2020 |
Jan 7 2020 12:00AM-1-FiscalCalendar | 1/7/2020 |
Jan 8 2020 12:00AM-1-FiscalCalendar | 1/8/2020 |
Jan 9 2020 12:00AM-1-FiscalCalendar | 1/9/2020 |
Jan 10 2020 12:00AM-1-FiscalCalendar | 1/10/2020 |
Jan 11 2020 12:00AM-1-FiscalCalendar | 1/11/2020 |
Jan 12 2020 12:00AM-1-FiscalCalendar | 1/12/2020 |
Jan 13 2020 12:00AM-1-FiscalCalendar | 1/13/2020 |
Jan 14 2020 12:00AM-1-FiscalCalendar | 1/14/2020 |
Jan 15 2020 12:00AM-1-FiscalCalendar | 1/15/2020 |
Jan 16 2020 12:00AM-1-FiscalCalendar | 1/16/2020 |
Jan 17 2020 12:00AM-1-FiscalCalendar | 1/17/2020 |
Jan 18 2020 12:00AM-1-FiscalCalendar | 1/18/2020 |
Jan 19 2020 12:00AM-1-FiscalCalendar | 1/19/2020 |
Jan 20 2020 12:00AM-1-FiscalCalendar | 1/20/2020 |
Jan 21 2020 12:00AM-1-FiscalCalendar | 1/21/2020 |
FACT_PAYMENT:
Amount | CustRecId | DueDateRecId | TransDateRecId |
315 | 5637145326 | Aug 1 2020 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
1000 | 5637146079 | Aug 1 2020 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
19.35 | 5637146079 | Aug 31 2020 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
420 | 5637145326 | Sep 1 2020 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
525 | 5637145326 | Oct 1 2020 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
4000 | 5637145326 | Nov 6 2020 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
600 | 5637146089 | Nov 9 2020 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
600 | 5637145326 | Jan 11 2021 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
-9.79 | 5637144576 | Mar 10 2021 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
174.95 | 5637144576 | Mar 10 2021 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
9.79 | 5637144576 | Mar 10 2021 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
-9.79 | 5637144576 | Mar 11 2021 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
9.79 | 5637144576 | Mar 11 2021 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
19.58 | 5637145327 | Mar 13 2021 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
-32.54 | 5637144576 | Mar 18 2021 12:00AM-1-FiscalCalendar | Aug 1 2020 12:00AM-1-FiscalCalendar |
349.9 | 5637144576 | Mar 18 2021 12:00AM-1-FiscalCalendar | Mar 13 2021 12:00AM-1-FiscalCalendar |
32.54 | 5637144576 | Mar 18 2021 12:00AM-1-FiscalCalendar | Mar 18 2021 12:00AM-1-FiscalCalendar |
0 | 5637144576 | Mar 18 2021 12:00AM-1-FiscalCalendar | Mar 18 2021 12:00AM-1-FiscalCalendar |
Hope this helps!
Nick
Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!
Check out the October 2025 Power BI update to learn about new features.