DAX code to check multiple selected values in a Treemap - powerbi

I am working on dynamically changing titles in PowerBI. I have Treemap which displays the population of 6 US states - 2 states from the east (NY, MA) and 3 from the west (CA, OR, WA).
The treemap looks like this -
A user can choose multiple states from a treemap by pressing ctrl and clicking on the square that represents the state.
When the user selects both the eastern states, I want the title to say "population in the east states" and similarly for the west too. Can this be done using DAX?
My title requirements -
If selected states are MA && NY then display "Population in the East States" as the title.
If selected states are CA && OR && WA then display "Population in the West States" as the title.
If only 1 state is selected then display the name of the state in the title.
I could do 3. Can someone please help me with 1 and 2?

Here's a measure that will do this.
Title =
SWITCH( TRUE(),
CONCATENATEX(VALUES(Table3[State]), Table3[State], ",") = "MA,NY",
"Population in the East States",
CONCATENATEX(VALUES(Table3[State]), Table3[State], ",") = "CA,OR,WA",
"Population in the West States",
HASONEVALUE(Table3[State]),
VALUES(Table3[State]),
"Population by State"
)
The VALUES function returns a list of the distinct values in the column specified.

Related

Table for two-group mean-comparison tests with estpost

Since I am a Stata beginner, I don't know how to import my data, but I just wanted some help to understand my mistake.
I would like to create a table where the first column displays the mean characteritics of California, the second column the mean characteritics of all other states (unweighted by their population), and the last column reports the p-value of the differences in means. The variable California is a dummy variable where 1 equals to California and 0 equals to 38 others states.
Here is the result :
estpost ttest cigsale lnincome beer age15to24 retprice, by(California)
esttab using ta.rtf, cell("mu_1 mu_2 p") label ///
title(Mean characteristics of California and 38 control states) ///
collabels(none) unstack noobs nonumber mtitles("California" "2" "P value") ///
addnote("All variables are averaged from 1980 to 1988. Beer consumption is averaged from 1984 to 1988. GDP per capita is measured in 1997 dollars, retail prices are measured in cents, beer consumption is measured in gallons, and cigarette sales are measured in packs")
Result
The problem is that when I put mtitles ("California" "Average of 38 control states" "P value"), it only works for the column California as you can see in the picture. The two other columns do not have the titles "Average of 38 control states" and "P value". I have tried many commands but there is only a title for the California column.
Could you please help me figure out what is going on?
This is one model with three columns, so instead of mtitles you can use collabels.
collabels("California" "2" "P value")

Get the last 5 results of column C or D if column A or B is equal to ___?

I know the title is a horrible description, sorry.
Basically I have a sheet with results from basketball games. So in column A I have the home team. In column B I have the away team. In column C the home team's points. In column D the away team's points. There's about 500 rows worth of data at the minute.
What I want to do is the following:
Say I want to get the average points scored by the New York Knicks in their last 5 games. The most recent games are at the bottom of the sheet, and the first/oldest ones at the top of the sheet.
So across the bottom/last 5 instances of "New York Knicks" in column A and B, I want the average of the results of C (if New York Knicks is in column A) and D (if in column B).
I know how to do this if I would want just the last 5 home games for instance (so in that instance I basically query the bottom 5 results of column C in the last 5 occurrences of column A being New York Knicks). I don't know how to do it when I am looking for when New York Knicks occurs in either column A or B, and then have to get the averages from column C or D.
Can anyone help?
this will transform your 4 columns into 2 columns:
=ARRAYFORMULA(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(A2:B<>"", "♠"&A2:B&"♦"&C2:D, )),,999^99)),,999^99), "♠")), "♦"))
and average score of the last 5 games:
=ARRAYFORMULA(AVERAGE(QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(A2:B<>"", "♠"&A2:B&"♦"&C2:D, )),,999^99)),,999^99), "♠")), "♦"),
"select Col2
where lower(Col1) contains 'new york knicks'
offset "&COUNTIF(A2:B, "new york knicks")-5)))

Create a monthly graph containing dynamical values

I discovered PowerBI few hours ago and I am trying to render a new graph, pretty complex from my data.
My subject:
I have a list of employees from my society with some information (name, location, seniority, ....) inside a table named Suivis de mission.
I'm focusing on seniority column. I have to display, monthly, the number of employees which has less than 3 seniority years, more than 3, more than 5 and more than 8.
This field is calculed through another field (date_de_changement_de_mission) by the next formula:
(today - date_de_changement_de_mission) / 365
It gives me a number according to the seniority years (above in french)
My experimental work:
I tried to make some things and this is what I have up to now.
I created a new table with months (string column1 and int column2) and the third column according to the number of employee with less than 3 seniority years for each month.
This is the formula that I'm trying to implement in order to get the number but I have lot of mistakes :
Nbr_inf_3_ans = SUMX('Suivis de mission';
IF(
'Suivis de mission'[Activité] = "En poste";1 &&
DIVIDE(
DATEDIFF(01/01/2019;'Suivis de mission'[Date de changement de mission].[Date];DAY);365)
;"null"))
The formula must contains several conditions :
Field 'Suivis de mission'[Activité] has to be : "En poste"
Loop over each month in my table (if the month is not targed, display 0)
I'm a bit lost.
Assuming that:
You want to do it in Power Query Editor
You already created the "seniority" and "month" ("mois*") columns
You are not worried about the year (you are really grouping just by month)
Then these steps may help you to begin:
Create a custom column name "IsLesserThan3" with the formula:
if [Seniority] < 3 and [Activité] = "En poste" than 1 else 0
Change the formula to include the requirement about "if the month is not targed" (i didn't get what it means, sorry).
Choose "Transform" (menu), then "Group By", then basic, set group by = month, give it a name ("Nbr_inf_3_ans"), set operation = sum, set column = IsLesserThan3
EDITED
New extra assumption:
There is a second table with month description and month number and you want to create the new column there ("Nbr_inf_3_ans") and fill it, probably to grant all the months are there (including those with no occurrences).
With this new assumption in mind, the approach will be to join the two tables and do the same thing:
Create a custom column named "IsLesserThan3" with the formula:
if [Seniority] < 3 and [Activité] = "En poste" than 1 else 0
Change the formula to include the requirement about "if the month is not targed" (i didn't get what it means, sorry).
Change the type of the new column to integer (click the icon to the left of the column name).
Create a custom column named "Mois_int" with the formula:
Date.Month([date_de_changement_de_mission])
Then join the tables by month. To do that, select the other table and choose "Transform" (menu), then "Merge", then let the second table selected and select the first table as the "bottom" table, select columns "Mois_int" in both tables, set "join kind" = "left outer".
PowerBI will create a new column wich name is the name of the first table. Click the arrows icon in that column header and choose "aggregate" and mark only "sum of LesserThan3".

Extract text before last space in PowerBI

I want to extract text before the last space from column A and add it to column B.
Example of input:
Chicago A12
New York GE8
United States of America AB8
Wanted output:
Chicago
New York
United States of America
ColumnB =
VAR string_length = LEN('Data'[ColumnA])
RETURN
TRIM(
LEFT(
SUBSTITUTE('Data'[ColumnA];" "; REPT(" "; string_length));string_length)
)
This does only work if I have one word before the space.
Output:
Chicago
New
United
Assuming you always have last 3 characters behind your Inpurt such as A12, GED or so on
Create New Field and add below expression to it.
Note: Assuming you are not having City column as Null or empty, else you will have to add one more condition of if
Expected Result = LEFT('Table'[City];LEN('Table'[City])-3)

Google geochart - arrow between countries

I need show arrow between countries in Google geochart. How do i do that?
Example : Chart "Number of tourists", Need to show the number of tourists flow between countries. One arrow from USA to Canada and another from Canada to USA, indicating the numbers on tooltips.