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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Matthieu_R
Frequent Visitor

Compare two dates from seprate table that are not directly connected

Hello guys,

 

I'm havig trouble while trying to create a measure that compare two dates from to fact tables that are not (obviously) connected. I will explain my schema.

 

First i have an invoice table that look like this :

 

invoice_idcreation_datedue_dateAmountNumber of payments expectedamount_payment1amount_payment2amount_payment3
101/04/202401/05/202420 €21010null
202/04/202402/05/202430 €130nullnull
303/04/202403/05/202425 €310105
404/04/202404/05/202435 €135nullnull

 

Then i have a payment table that is filled automatically with the list of expected payments.

 

payment_idinvoice_idpayment_dateAmount_paid
1130/04/202410 €
2110/05/202410 €
3230/04/202430 €
4330/04/202410 €
5310/05/202410 €
63null5 €
74null35 €

 

If i don't have receive the payments the payment date is null. Then that column is update when i receive the payment effectively.

 

Now my goal is to count the number of payment that are received before the due date of each invoice. Note that the due date is the maximum date where we should receive all payments of an invoice.

 

Both table are not linked directly together but are linked via a dimension table that contains the invoice_id, the due_date, the amount etc...

And also both dates (due_date and payment_date) are connected to a calendar table (inactive relationships)

 

One of the solution would be to put the due_date in the payment table for each line. I think it will make everything easyer.

But is it a good practice to have one column from a dimension table that is present in multiple fact table ?

 

Thanks in advance for your help.

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

My approach to this would be to merge due date into the payment table in Power Query and then create a custom column that tests if the payment date is less than the due date. If the results of that column were 1 for less than and 0 for not then your resulting measure would be the sum of that column. If you used 'yes' for less than and 'no' for not your resulting measure would be a conditional count. 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

3 REPLIES 3
Matthieu_R
Frequent Visitor

will try that. Thanks

Here is an example code in case you need it...

let
    invoiceSource =
    Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc3dCcAgDEbRVUqeBfM7TXCZztOpOoka1LQPylU4fO5AUEArVUbWkZbJeL33M2Mcwn1BKx5fWjnVSdmK4jHBMhJG0pxkW0Z+SxZMg2myk2LfKdtTrQM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [invoice_id = _t, creation_date = _t, due_date = _t, Amount = _t, #"Number of payments expected" = _t, amount_payment1 = _t, amount_payment2 = _t, amount_payment3 = _t]),
    invoiceTypeChange = 
    Table.TransformColumnTypes(invoiceSource,{{"invoice_id", Int64.Type}, {"creation_date", type date}, {"due_date", type date}, {"Amount", type text}, {"Number of payments expected", Int64.Type}, {"amount_payment1", Int64.Type}, {"amount_payment2", Int64.Type}, {"amount_payment3", Int64.Type}}),     
    paymentSource = 
    Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAWMTfWMDfSMDIxMQ30DhUdMapVidaCUjqLSpviE2aWMg1whNtzFCGszFbbgpVBqH4WZQaZAKuKA5yDiIoDFUNBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [payment_id = _t, invoice_id = _t, payment_date = _t, Amount_paid = _t]),
    paymentTypeChange = 
    Table.TransformColumnTypes(paymentSource,{{"payment_id", Int64.Type}, {"invoice_id", Int64.Type}, {"payment_date", type date}, {"Amount_paid", type text}}),
    mergeQueries = 
    Table.NestedJoin(paymentTypeChange, {"invoice_id"}, invoiceTypeChange, {"invoice_id"}, "invoiceTable", JoinKind.LeftOuter),
    expandDueDate = 
    Table.ExpandTableColumn(mergeQueries, "invoiceTable", {"due_date"}, {"due_date"}),
    addColumn = 
    Table.AddColumn(expandDueDate, "Payment Before Due", each try if [payment_date] < [due_date] then 1 else 0 otherwise 0, Int64.Type)
in
    addColumn



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





jgeddes
Super User
Super User

My approach to this would be to merge due date into the payment table in Power Query and then create a custom column that tests if the payment date is less than the due date. If the results of that column were 1 for less than and 0 for not then your resulting measure would be the sum of that column. If you used 'yes' for less than and 'no' for not your resulting measure would be a conditional count. 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.