The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hi comunnity,
I need help with this problem. I have two simple tables.
The first table contains a list of transports from where - to where (columns "C" - "D") on a given day (column „B“).
The second table contains kilometers (column "D") valid in a given period (columns "E" - "F") for a given destination (columns "B" - "C").
Different kilometers on different dates are due to the detour.
I need to upload the correct kilometers for a given date and destination into the first table.
Thank you Ondřej
Solved! Go to Solution.
You can create a calculated column in the first table like
Distance =
SELECTCOLUMNS (
FILTER (
Table2,
Table2[ID] = Table1[ID]
&& Table2[From] = Table1[From]
&& Table2[To] = Table2[To]
&& Table2[Valid From] <= Table1[Date]
&& (
ISBLANK ( Table2[Valid To] )
|| Table2[Valid To] >= Table1[Date]
)
),
"@value", Table2[Distance]
)
Try
Distance =
SELECTCOLUMNS (
TOPN (
1,
FILTER (
Table2,
Table2[ID] = Table1[ID]
&& Table2[From] = Table1[From]
&& Table2[To] = Table2[To]
&& (
( Table2[Valid From] <= Table1[Date]
&& Table2[Valid To] >= Table1[Date] )
|| ( ISBLANK ( Table2[Valid To] ) && ISBLANK ( Table2[Valid From] ) )
)
),
Table2[Value To], DESC
),
"@value", Table2[Distance]
)
Open DAX Query View and run
EVALUATE
GENERATE (
Table1,
FILTER (
Table2,
Table2[ID] = Table1[ID]
&& Table2[From] = Table1[From]
&& Table2[To] = Table2[To]
&& Table2[Valid From] <= Table1[Date]
&& (
ISBLANK ( Table2[Valid To] )
|| Table2[Valid To] >= Table1[Date]
)
)
)
ORDER BY Table1[Delivery]
That will show you for every row in Table1 all the rows which would be returned from Table2. That should help you identify why multiple rows are being returned.
Hi @johnt75,
could i ask you to modify the formula? I had to adjust the logic in Table2 a little.
Newly, the date will not be filled in for standard kilometers in the mileage records (columns "E" and "F").
In the period of non-standard kilometers, the date will be filled in.
Will it be possible to add kilometers according to the date of the transport as follows?:
1. If it is in the date period, the given km is selected.
2. If it is outside the date period, the km without date (standard kilometers) is selected.
Thanks a lot for your help.
Ondřej
Try
Distance =
SELECTCOLUMNS (
TOPN (
1,
FILTER (
Table2,
Table2[ID] = Table1[ID]
&& Table2[From] = Table1[From]
&& Table2[To] = Table2[To]
&& (
( Table2[Valid From] <= Table1[Date]
&& Table2[Valid To] >= Table1[Date] )
|| ( ISBLANK ( Table2[Valid To] ) && ISBLANK ( Table2[Valid From] ) )
)
),
Table2[Value To], DESC
),
"@value", Table2[Distance]
)
Hi @johnt75,
I ran DAX Query View and found that the data is being assigned incorrectly according to the date from table2.
In the common table I see this:
However, this row from table2 should have been used in the given date:
Thank you
Ondřej
Those dates look like they are stored as strings. Use Power Query to turn them into date type.
Thank you 👍, switching the column format to date helped.
Ondřej
You can create a calculated column in the first table like
Distance =
SELECTCOLUMNS (
FILTER (
Table2,
Table2[ID] = Table1[ID]
&& Table2[From] = Table1[From]
&& Table2[To] = Table2[To]
&& Table2[Valid From] <= Table1[Date]
&& (
ISBLANK ( Table2[Valid To] )
|| Table2[Valid To] >= Table1[Date]
)
),
"@value", Table2[Distance]
)
Thank you Johnt75
When I adjust the distance to the destination in a given period, like this:
An error is displayed. If I leave the distances as in the previous case, 20km was incorrectly added everywhere, on some days it should have been 38km.
It tells me that a table containing multiple values was entered but only one value was expected.
But at first glance I don't see that I have any duplication anywhere. The dates in my mileage table don't overlap but follow each other.
Ondřej