How to get time from time dimension with a specific relationship? - powerbi

My data
Fact
iddimUser iddimtime
123 81200
124 84500
DimTime
iddimtime FullTime
81200 08:12:00.0000000
84500 08:45:00.0000000
Desired Table calculation
iddimUser iddimtime Time
123 81200 08:12:00.0000000
124 84500 08:45:00.0000000
I require to bring to the fact table a specific time from the time dimension base on one of the multiple relationships I have (I have several IdTimes in the fact table).
I tried this column calculation but getting an "A table of multiple values was supplied where a single value was expected" error
FullTime = TIME(
HOUR(CALCULATE( VALUES(DimTime[FullTime] ),
USERELATIONSHIP ( Fact[iddimtime], DimTime[iddimtime]))),
MINUTE(CALCULATE( VALUES(DimTime[FullTime] ),
USERELATIONSHIP ( Fact[iddimtime], DimTime[iddimtime]))),
SECOND(CALCULATE( VALUES(DimTime[FullTime] ),
USERELATIONSHIP ( Fact[IniciaProce], DimTime[iddimtime]))))

FullTime1 =
FORMAT(RELATED(DimTime[FullTime]),"hh:nn:ss")
This you can use as alternative to your measure and it will be much faster.
FullTime2 =
FORMAT(
LOOKUPVALUE(
DimTime[FullTime]
,DimTime[iddimtime]
,[iddimtime]
)
,"hh:nn:ss"
)

Why do you use the userelationship if there is only one active relationship between fact and dim tables ? All you need is the related function to access the related row in the dim table:
Note:
Ensure that The [iddimtime] column is the one who creates the relationship between both tables.
Related() is an iterator. Never use it with a calculate involving USERELATIONSHIP() in a calculated field.
RELATED() is used to access the column values from many side to one-side. Conversely, RELATEDTABLE() is used to access columns from one side to many side.
Please test this, and let me know if It solves your problem.
FullTime =
TIME ( HOUR ( RELATED ( DimTime[FullTime] ) ), MINUTE ( RELATED ( DimTime[FullTime] ) ), SECOND ( RELATED ( DimTime[FullTime] ) ) )

Related

Power BI: How to create a slider that slices multiple fields without becoming hierarchical?

I have a few different fields that are related: "total shares", "private shares", and "general shares". These are continuous fields (i.e., values can be 1,2,3,4,...).
Currently, I have a slider set up to slice "total shares" but I want it to slice all three of those fields at the same time. In essence, visuals shouldn't display shares above the sliced amount just because they're technically "private shares" and not "total shares", an issue we're currently facing.
I've tried simply adding the fields into the slicer, but then it becomes a drop down where we select which one should be filtered. That's not what we're looking for either.
Another thing I tried is this:
shares_aggregate =
ALL(
merged_data[general_shares],
merged_data[private_shares],
merged_data[shared_files_from_storage],
merged_data[shared_files_total]
)
From there I'd apply a filter to shares_aggregate. PowerBi doesn't accept this though, because apparently I'm misunderstanding how ALL works.
Thanks for any help!
Please try this, and let me know if It solves your problem.
shares_aggregate =
UNION (
DISTINCT ( merged_data[general_shares] ),
DISTINCT ( merged_data[private_shares] ),
DISTINCT ( merged_data[shared_files_from_storage] ),
DISTINCT ( merged_data[shared_files_total] )
)

Date Filter not working with CALCULATE, FILTER, then ALLSELECTED in Power BI

Description:
I need help trying to figureout my issue with a visual not displaying after I use a date filter. Below is my simple measure for counting the unique ID while filtering ALLSELECTED within the given Year/Month (202111). My result is a running total that doesn't care about the year so that I can keep it adding for any given time range, instead of it splitting per year (which is the default calculation). I can't use ALL in my FILTER because none of the page filters will function.Blank visual when filtering CreatedDt only.
Blank visual when filtering CreatedDt only.
Problem:
My problem is that when I have a change my date range filter (CreatedDt Filter) everything in my visual goes blank, but when I use any other filter (i.e. Carrier, Agent, etc.) those will work for filtering. It seems that its only the date filter and when the measure has ALLSELECTED in it.
Measure Calculation:
QuoteRequestId running total in CreatedDtMonth =
CALCULATE (
COUNT( 'vw_rpt_AllSubmissions'[QuoteRequestId] ),
FILTER (
ALLSELECTED ( 'vw_rpt_AllSubmissions' ),
'vw_rpt_AllSubmissions'[CreatedDtYearMonth] <=
MAX('vw_rpt_AllSubmissions'[CreatedDtYearMonth] )
)
)
I have actually found my own solution to this issue, see below for my answer:
Calculation Solution:
CALCULATE ( COUNT( 'vw_rpt_AllSubmissions'[QuoteRequestId] ), FILTER ( ALLEXCEPT( vw_rpt_AllSubmissions,vw_rpt_AllSubmissions[PayToCarrierName],vw_rpt_AllSubmissions[AgencyName], vw_rpt_AllSubmissions[AgentFullName],vw_rpt_AllSubmissions[CreatedDt] ), 'vw_rpt_AllSubmissions'[CreatedDtYearMonth] <= MAX ( 'vw_rpt_AllSubmissions'[CreatedDtYearMonth] ) ) )
Solution Description:
I have come up with the above work around that allows me to add the listed Page Filters into my DAX calculated column so that it manually accepts what is being used. This isn't the best case scenario because I'll have to add it each time that I change my page filters but I won't be doing that often. I hope someone else can benefit from this solution and please leave any comments if a better solution comes up.

Power BI - Getting the most recent value from a related table

I know this must be extremely simple, but every example I can find online only works within a single table. I've simplified my situation to these two tables:
I want to add a calculated column to the first table, showing the most recent value for that id. It also needs to work with text.
There are a variety of ways to do this kind of thing as I've explained before and all of the solutions there can be adjusted to work in this case.
Doing this as a calculated column and with a second table, you need to make sure you are using row context and filter context appropriately.
Here's are a couple different possibilities I think may work:
MostRecentValue =
MAXX ( TOPN ( 1, RELATEDTABLE ( Table2 ), Table2[date] ), Table2[value] )
In this one, RELATEDTABLE is doing the work of filtering Table2 to only the rows where id matches Table1.
MostRecentValue =
VAR PrevDate = CALCULATE ( MAX ( Table2[date] ) )
RETURN CALCULATE ( MAX ( Table2[value] ), Table2[date] = PrevDate )
The relationship is more subtle here. Wrapping the MAX in CALCULATE forces a context transition so that the row context (which includes id) is applied to Table2 as filter context.

PowerBI DAX get COUNT DISTINCT with GROUP BY , see SQL query below

I have got this following SQL query that gives me the correct value from the database.
SELECT
SUM( DISTINCT_ORDER_NUMBERS )
FROM
(
SELECT STORE_KEY,
COUNT( DISTINCT TRANSACTION_NUM ) AS DISTINCT_ORDER_NUMBERS,
DATE_KEY,
TRANSACTION_TYPE_KEY
FROM Pos_Data
GROUP BY STORE_KEY,
DATE_KEY,
TRANSACTION_TYPE_KEY
)
AS A
I am however facing challenges writing a DAX formula for a measure in Power BI Here is what I have tried so far but I get an error.
Total Number Of Orders
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUM(_TotalOrders[DISTINCT_ORDER_NUMBERS])
Please assist
The SUM function expects a base table rather than a calculated table.
Try this instead:
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUMX(_TotalOrders, [DISTINCT_CHECK_SEQ])
Edit: If the difference you mentioned is related to nulls, then try this in place of DISTINCTCOUNT.
COUNTAX( DISTINCT( 'Pos_Data'[TRANSACTION_NUM] ), 'Pos_Data'[TRANSACTION_NUM] )
The COUNTAX function (as opposed to COUNTX) does not count nulls.

Calculate value if outside time range

I have a problem where I need to figure out if a project has values outside it's start and finish date range.
Below is a simple relationship of dimension table containing start and finish dates of the projects. And a fact table containing time registration.
The table below has a column 'Outside Date Range' Which I'd like to have a true/false value. for example if Main2 Table contains a date Monday, May 13, 2018. The column should show false.
I tried something like
Outside Date Range = CALCULATE(SUM(Main2[Value]), FILTER(Main2, Main2[Time] < LOOKUPVALUE(Main[Start], Main[Project], ALL(Main2[Project]))))
But not really sure how to approach the relationship between the two tables properly.
The two approaches I would suggest are either a calculated column or a measure.
Calculated column:
Outside Date Range =
VAR rowsOutsideRange =
CALCULATE (
COUNTROWS ( Main2 ),
FILTER (
RELATEDTABLE ( Main2 ),
Main2[Time] < Main[Start]
|| Main2[Time] > Main[Finish]
)
)
RETURN
IF ( rowsOutsideRange > 0, TRUE (), FALSE () )
You were pretty close in your solution! Since you have a relationship between the two tables RELATEDTABLE will only return the related rows which removes the necessity of a LOOKUPVALUE(). Also, counting the rows is sufficient since we only want to know if any rows exist outside of the range, not how many.
You could also create a measure:
Outside Date Range Measure :=
VAR rowsOutsideRange =
CALCULATE (
COUNTROWS ( Main2 ),
FILTER (
Main2,
Main2[Time] < MIN ( Main[Start] )
|| Main2[Time] > MAX ( Main[Finish] )
)
)
RETURN
IF ( rowsOutsideRange > 0, TRUE (), FALSE () )
Which is pretty similar to the calculated column, the only this is we need to aggregate the start and finish dates. On its own this measure doesn't have any value, it needs to be sliced by a project to be correct. If you would really want to you could use a SUMX() type of construction to create an overall TRUE/FALSE statement which tells you if any of the project have rows outside their ranges but for your use case I don't see the benefit of that.
The choice between a calculated column and a measure is dependent on the legibility of the code and resource usage. A calculated measure uses more memory and a measure uses more CPU.
Looking at your case I would go for a calculated column, which seems the most simple and clear solution.
Hope that helps!
Jan