CALCULATETABLE with OR statement in powerBI - powerbi

How do you make an OR statement in a calculate table query.
I have a statement where I select data from all rows of the city Davison but I not only want info from Davison, I also want information from the city Flint. How do I do this.
Table = CALCULATETABLE(datasetnew, datasetnew[City] = "Davison")
This is it, but i want something like this.
Table = CALCULATETABLE(datasetnew, datasetnew[City] = "Davison", or datasetnew[City] = "Flint")

Try this.
Table = CALCULATETABLE(datasetnew, datasetnew[City] IN {"Davison", "Flint" })

As David mentioned IN statement in DAX is nice and short, but one other way of writing it is as follows(using OR operator ||(double pipe))
Table = CALCULATETABLE(datasetnew, datasetnew[City] = "Davison" || datasetnew[City] = "Flint")

Related

How to do left outer join of two tables without any relationship in DAX using naturalLeftJoin or Generate all functions

I have 2 sample tables, Table "EX4" and "Dates". Now I want to create a third table in DAX which joins dates with EX4 table.
Now I want to join EX4 table with Dates[Sale_Date] where
Dates[Sale_Date] >= Ex4_Ext[min_date] and Dates[Sale_Date] <= Ex4_Ext[max_Date]
The output should be as follows:
I am using below DAX code and is not giving the correct output because its not doing left join. It is doing Cartesian product.
Ex4_Ext =
var tbl = SELECTCOLUMNS(EX4,"min_date",EX4[Min_Date],"max_Date",EX4[Sale_Date])
var tbl2 = SELECTCOLUMNS(Dates,"sale_date1",date(year(Dates[Sale_Date]),MONTH(Dates[Sale_Date]),Day(Dates[Sale_Date])))
Return
generate(tbl,tbl2)
I Cannot have a relationship between these two tables..Neither Generate all nor Naturalleftjoin is working..
Any help will be appreciated..
After a long hit and try method I found the solution. Below DAX query can join without relationship:
Ex4_Ext =
var tbl = SELECTCOLUMNS(EX4,"min_date",EX4[Min_Date],"max_Date",EX4[Sale_Date])
var tbl2 = generate(tbl,DATESBETWEEN(Dates[Sale_Date],[min_date],[max_Date]))
Return
SELECTCOLUMNS(tbl2,"Min_Date",[min_date],"Max_Date",[max_Date],"Sale_Date",DATE(year([Sale_Date]),month([Sale_Date]),1))
Got help from youtube video : https://www.youtube.com/watch?v=U_7mFutB5OM
Generate function when used with DATESBETWEEN function acts as Inner join

Show all rows as default in calendar in Oracle Apex

I'm creating a report table type calendar where users can create back up by date select a filter that would filter out the table values depending on the user selected. (i.e. if they choose user1, then only back ups with user1 will show up)
I would like it to be when P106_BACK_UP_BY_USER = 0, the table shows all the values (aka getting rid of the "where" portion of the query.
Thank you for your help!
I'm having issues with trying to allow the user to see all the back ups of the table again (getting rid of the filtered value). My current query is this:
I would like it to be when P106_BACK_UP_BY_USER = 0, the table shows all the values (aka getting rid of the "where" portion of the query.
Thank you for your help!
You can use case when statements in your query's where condition as follows:
select *
from my_table
where my_table.created_by =
(select user_name from my_table2 where app_users_id =
case :P106_BACKUP_BY_USER when 0 then app_users_id
else :P106_BACKUP_BY_USER
end)
And for getting better help, please paste your code as text not as an image next time.
This should work too:
...
WHERE b.active_server = s.server_id
AND (:P106_BACK_UP_BY_USER = 0 OR
UPPER(b.created_by) =
(SELECT UPPER(user_name)
FROM eba_bt_app_users
WHERE app_users_id = :P106_BACK_UP_BY_USER
)
);

Distinct count of users with net balance > 0

My data looks like this.
I tried with below DAX but I am getting an error:
Distinct Users = CALCULATE(DISTINCTCOUNT(MyList[User]),SUM(MyList[Balance]) > 0)
Error message: A function 'SUM' has been used in a True/False
expression that is used as a table filter expression. This is not
allowed.
You could do this by using a calculated column to evaluate the balance and then use that to calculate the measure:
Column:
Distinct Users Test =
VAR User = MyList[User]
RETURN CALCULATE(SUM(MyList[Balance]),
FILTER(MyList,MyList[User]=User))
Measure:
Distinct Users = CALCULATE(DISTINCTCOUNT(MyList[User]),
MyList[Distinct Users Test]>0)
Hope this helps.
Edit: If you want it as a single measure, You can make use of summarize and calculatetable:
Distinct Users New = COUNTX(
CALCULATETABLE(SUMMARIZE(MyList,MyList[User],
"Bal",
SUM(MyList[Balance]))),
[Bal]>0)

In Power BI, how can you return the category label based on the max value for all categories returned by a measure using DAX?

Looking for some DAX guidance here, to return a text field based on the maximum returned by a measure appplied over that column.
i.e we have table[category_column] and [measure]
and the measure references columns in other tables, with two-sided relationships.
Any help much appreciated, very stuck!
Thanks in advance
If I'm reading right, I think you want to draw the measure against the column values and return the row with the max of the measure. Something like this?
newMeasure =
VAR vals = SUMMARIZE('Table', 'Table'[Column1], "myMeasure", [Measure])
VAR measureMax = MAXX(vals, [myMeasure])
VAR value = CALCULATE(MAXX(FILTER(vals, [myMeasure] = measureMax), [Column1]))
RETURN
value
If your column to be used for max is Column1 and your text field is Column2 something like the below could work:
Measure =
VAR Max_Value =max('Table'[Column1])
Return CALCULATE(MAX('Table'[column2]),'Table'[Column1]=Max_value)
If this is not what you are looking for you have to explain your requirements in further detail, hopefully with examples.

How to display columns depending on the date selected [From and To] in rails?

|Jan_2016|Feb_2016|Mar_2016|Apr_2016|May_2016|June_2016|July_2016|Aug_2016|Sept_2016|Oct_2016|Nov_2016|Dec_2016|
"From" and "To" filter are there in which we have to select dates. Depending upon the values selected, we have to select columns dynamically which lies between selected date filters.
These are different columns. I have to display columns depending on the date selected in From and To datepicker in rails.
eg: Suppose "From:=>Jan_2016" and "To=>June_2016" then we have to dsplay all columns between these two dates[like => |Jan_2016|Feb_2016|Mar_2016|Apr_2016|May_2016|June_2016|].
Thanks.
Try this. It is not the best possible solution but it may work.
start_date = params[:to]
end_date = params[:from]
columns = Model.column_names
# For date filters
start_date_index = columns.index(start_date)
end_date_index = columns.index(end_date)
result = columns[start_date_index..end_date_index]
This is your solution
current_date = from.to_date
result = []
while ( current_date <= to.to_date )
result << current_date.strftime("%b_%Y")
current_date = current_date.next_month
end
result contains the columns you have to display