Find value in another table - Power BI (DAX) - powerbi

I'm trying to add a column in Power BI to see if a value appears in another table, without taking the case.
For example from the
Booking Table:
User
Domain
exx
domain1
EPO
domain2
POA
domain3
ER
domain4
and
Log Table:
LogName
Date
RT
12:31
EXX
11:23
POA
9:11
rtt
10:11
I want to see if the User appears in LogName and I'd like to have the output column3:
User
Domain
Column3
exx
domain1
1
EPO
domain2
0
POA
domain3
1
ER
domain4
0
I tried
CALCULATE(COUNT('Booking Table'[User]),
FILTER('Log Table', LOWER('Log Table'[LogName]) = LOWER('Booking Table'[User])
||
LOWER('Log Table'[LogName]) = LOWER('Booking Table'[User])
)
)
but apparently it doesn't work properly.
Thanks in advance for your help!

Measure =
var tbl = SUMMARIZE('Booking Table','Booking Table'[User],'Booking Table'[Domain],'Log Table'[LogName])
var count_user = COUNTX(tbl,'Log Table'[LogName])
return
IF(ISEMPTY(VALUES('Log Table'[LogName])),0,count_user)
Considering the relationship between the two tables is on User & LogName

Related

Merging Column results to as a parameter in paginated reports

I would like to separate Dublin to other cities like Manchester, St Albans (grouped as UK). How should I do this? There's no country data for this
I have tried adding the city from query designer, and editing the available values as "specify values" is not working
Can you create a new DAX measure column , with a condition such as
NewCountryColumn = IF(Table[Location] = 'Dublin', 'IRELAND', 'UK')
Then you'll see a new measure column, which will have IRELAND for DUBLIN and rest all will be shown as UK , use this column to do your filter.

Active users on a given date in a Month in Power BI

I am working to get cumulative distinct count of uids on daily basis. My dataset consists dates and UserIDs active on that date. Example : Say there are 2 uids (235,2354) appeared on date 2022-01-01 and they also appeared on next day with new uid 125 (235,2354,125) on 2022-01-02 At this point i want store cumulative count to be 3 not 5 as (user id 235 and 2354 already appeared on past day ).
My Sample Data looks like as follows:
https://github.com/manish-tripathi/Datasets/blob/main/Sample%20Data.xlsx
enter image description here
and my output should look as follows:
enter image description here
Here's one way that seems to work, using your linked Excel sheet as the data source.
Create a new table:
Table 2 = DISTINCT('Table'[Date])
Add the columns:
MAU = CALCULATE(
DISTINCTCOUNT('Table'[User ID]),
'Table'[Date] <= EARLIER('Table 2'[Date]))
DAU = CALCULATE(DISTINCTCOUNT('Table'[User ID]),
'Table'[Date] = EARLIER('Table 2'[Date]))
Result from your Excel data

Power BI - Filtering model on latest version of all attributes of all dimensions through DAX

I have a model that's comprised of multiple tables containing, for every ID, multiple rows with a valid_from and valid_to dates.
This model has one table in that is linked to every other table (a table working as both a fact and a dimension).
This fact has bi-directional cross filtering with the other tables.
I also have a date dimension that is not linked to any other table.
I want to be able to calculate the sum of a column in this table in the following way:
If a date range is selected, I want to get the sum of the latest value per ID from the fact able that is before the max selected date from the date dimension.
If no date is selected, I want to get the sum of the current version of the value per ID.
This comes down to selecting the latest value per ID filtered on the dates.
Because of the nature of the model (bi-directional with the fact/dimension table), I want to have the latest version of any attribute from any dimension selected in the visual.
Here's an data example and the desired outcome:
fact/dimension table:
ID
Valid_from
Valid_to
Amount
SK_DIM1
SK_DIM2
1
01-01-2020
05-12-2021
50
1234
6787
1
05-13-2021
07-31-2021
100
1235
6787
1
08-01-2021
12-25-2021
100
1236
6787
1
12-26-2021
12-31-2021
200
1236
6787
1
01-01-2022
12-31-9999
200
1236
6788
Dimension 1:
ID
SK
Valid_from
Valid_to
Name
1
1234
10-20-2019
06-01-2021
Name 1
1
1235
06-02-2021
07-31-2021
Name 2
1
1236
08-01-2021
12-31-9999
Name 3
Dimension 2:
ID
SK
Valid_from
Valid_to
Name
1
6787
10-20-2019
12-31-2021
Name 1
1
6788
01-01-2022
12-31-9999
Name 2
My measure is supposed to do the following:
If no date is selected than the result will be a matrix like the following:
Dim 1 Name
Dim 2 Name
Amount Measure
Name 3
Name 2
200
If July 2021 is selected than the result will be a matrix like the following:
Dim 1 Name
Dim 2 Name
Amount Measure
Name 2
Name 1
100
So the idea here is that the measure would filter the fact table on the latest valid value in the selected date range, and then the bi-directional relationships will filter the dimensions to get the corresponding version to that row with the max validity (last valid row) in the selected range date.
I have tried to do the following two DAX codes but it's not working:
Solution 1: With this solution, filtering on other dimensions work and I get the last version in the selected date range for all attributes of all used dimensions. But the problem here is that the max valid from is not calculated per ID, so I only get the max valid from overall.
Amount Measure=
VAR _maxSelectedDate = MAX(Dates[Dates])
VAR _minSelectedDate = MIN(Dates[Dates])
VAR _maxValidFrom =
CALCULATE(
MAX(fact[valid_from]),
DATESBETWEEN(fact[valid_from], _minSelectedDate, _maxSelectedDate)
|| DATESBETWEEN(fact[valid_to], _minSelectedDate, _maxSelectedDate)
)
RETURN
CALCULATE(
SUM(fact[Amount]),
fact[valid_from] = _maxValidFrom
)
Solution 2: With this solution, I do get the right max valid from per ID and the resulting number is correct, but for some reason, when I use other attributes from the dimensions, it duplicates the amount for every version of that attribute. The bi-directional filtering does not work anymore with Solution 2.
Amount Measure=
VAR _maxSelectedDate = MAX(Dates[Dates])
VAR _minSelectedDate = MIN(Dates[Dates])
VAR _maxValidFromPerID =
SUMMARIZE(
FILTER(
fact,
DATESBETWEEN(fact[valid_from], _minSelectedDate, _maxSelectedDate)
|| DATESBETWEEN(fact[valid_to], _minSelectedDate, _maxSelectedDate)
),
fact[ID],
"maxValidFrom",
MAX(fact[valid_from])
)
RETURN
CALCULATE(
SUM(fact[Amount]),
TREATAS(
_maxValidFromPerID,
fact[ID],
fact[valid_from]
)
)
So if somebody can explain why the bi-directional filtering doesn't work anymore that will be great, and also, more importantly, if you have any solution to have both the latest value per ID and still keep filtering on other attributes, that would be great!
Sorry for the long post, but I thought it's best to give all the details for a complete understanding of my issue, this has been picking my brain since few days now and I'm sure I'm missing something stupid but I turned to this community for help because I cannot seem to be able to find a solution!
Thank you very much in advance for any help!
Seems to be workable with a dummy model. I didn't got the point how filter ID, so if it creates a problem let me know how you handle ID. Then I changed fact to facts as fact is a function. Also, I'm not sure about the workability of the measure at your real model. Hope you will give some feedback.
Amount Measure =
VAR ValidDate=
calculate(
max(facts[Valid_to])
,ALLEXCEPT(facts,facts[ID])
,facts[Valid_to]<=MAX(Dates[Date])
)
Return
CALCULATE(
SUM(facts[Amount])
,TREATAS({ValidDate},facts[Valid_to])
)

Create a filter which show the last 12 hours data power bi

I want to create a filter in power bi which first shows the data of the last 12 hours and the second the history of the data based on my datetime field named "open_time".
For example :
When I click on "now", I want to display the data of the last 12 hours based on open_time field in a table visualization and when o click on history, I have a date filter where I can choose one date and one hour.
Sorry for my English, hope you understand my problem.
Regards
You can create an unconnected table with labels ("Now", "Historic") to use as a slicer.
Then we can create measuers
CheckTime =
var __selectedType = SELECTEDVALUE(disconnected[tagList])
var __DateForFilter = if( __selectedType = "a", TODAY() + TIME(HOUR(NOW()) - 4, MINUTE(NOW()), SECOND(NOW())) , NOW())
return
calculate( SUM(DateTimeTab[val] ), FILTER(ALL(DateTimeTab), DateTimeTab[Data] >= __DateForFilter ))

powerbi directquery distinct count group by issue

I have a table format with
record date(including seconds), user ID, Database
I need to show the maximum distinct number of users per hour grouped by every 5 minutes ( not sure if this explains - please see example below)
I am only using DirectQuery storage and not intended to change that to import as well.
I have tried various methods but could not manage without changing the storage mode. Any help is appreciated.
My table sample is,
21/01/2019 12:35:00, jane, UK
21/01/2019 12:35:00, joe, UK
21/01/2019 12:35:00, joe, NL
21/01/2019 12:40:00, bob, NL
21/01/2019 12:40:00, jane, NL
21/01/2019 12:40:00, joe, NL
21/01/2019 12:40:00, jakob, NL
Expected result
21/01/2019 12, UK, 2
21/01/2019 12, NL, 4
Start by adding an hour column in the data table.
Hour = HOUR([DateTime])
Then create a temporary summarized table (this table can be hidden from the report view).
SummTemp =
SUMMARIZE(
Data;
[Datetime];
[Nat];
[Hour]; // this is the calculated column with only hour.
"Date"; DATE(YEAR([DateTime]); MONTH([DateTime]); DAY([DateTime]));
"Count"; COUNTA('Data'[Nat])
)
Then create a second summarized table:
SummaryTable =
SELECTCOLUMNS(
SUMMARIZE(
SummTemp;
[Date];
[Hour];
[Nat];
"MaxCount"; MAX('SummTemp'[Count]);
"NewName"; [Date]&" "&[Hour]
// OR "NewName"; ( [Date]&" "&TIME([Hour]; 0; 0))*
);
"DateHour"; [NewName];
"Nationality"; [Nat];
"MaxCNX"; [MaxCount]
)
The end result will look like this:
*If you use this line, then you will get a column in text format but with [dd/mm/yyyy hh:00:00]. You can convert this column to a datetieme format by selecting it and change it from text to datetime via Modeling->Data type. NMot sure if you can drop both minutes and seconds from it but you can drop seconds so that your DateHour column will have a format like: [dd/mm/yyyy hh:00]