Crosstab data in Power BI - powerbi

I have data in the PowerBI which I need to crosstab in order to generate the base data for a specific visual.
Example data:
tblExample = DATATABLE("Customer ID", INTEGER, "Gender", STRING, "Age Range", STRING, "Order Date", DATETIME, "WS Next Day", STRING, "Order Next Day", STRING, "WS Next Week", STRING, "Order Next Week", STRING, "WS Next Month", STRING, "Order Next Month", STRING,
{
{1, "Female", "30 - 39", "2017-02-09", "Yes", "No", "Yes", "No", "No", "No" },
{2, "Female", "30 - 39", "2017-02-11", "Yes", "Yes", "Yes", "No", "No", "No" },
{3, "Female", "50 - 59", "2017-02-12", "Yes", "No", "Yes", "No", "No", "No" },
{4, "Male", "20 - 29", "2017-02-12", "Yes", "No", "Yes", "No", "No", "No" },
{5, "Male", "40 - 49", "2017-02-19", "No", "No", "Yes", "No", "No", "No" }
}
)
This data shows customers who have placed an order and then a set of six yes / no flags showing whether or not the customer has visited the web site again the following day / week / month, and whether this visit resulted in another order.
What I need to do is turn this information into a table with one row for each category of "Next Day", "Next Week" and "Next Month", and for each row a value showing the count of customers who (a) visited and (b) purchased.
I thought this would be pretty straight forward to do in DAX - and to be honest I'm not sure that it isn't and I haven't just missed something really obvious - but at the moment I can't see a tidy way to achieve this.
I have created measures for each of the six values I need as follows:
NextDay_Visits = COUNTROWS(FILTER(tblExample, [WS Next Day] = "Yes"))
NextDay_Orders = COUNTROWS(FILTER(tblExample, [Order Next Day] = "Yes"))
And so on, and when I look at these measures in a card they give me the correct values, but to jam these into a table which is the correct shape I do the following (which is almost certainly the wrong way to do this in PowerBI !):
1 Create a new table
tblJunk1 = DATATABLE("Row ID", INTEGER, "Category", STRING, { {1, "Next Day"} })
2 Add the appropriate measures to the new table as new columns
tblJunk2 = ADDCOLUMNS(tblJunk1, "Visits", [NextDay_Visits], "Purchases", [NextDay_Purchases])
3 Do the same two steps again using tblJunk3 / tblJunk4 for the "Next Week" figures
4 Do the same two steps again using tblJunk5 / tblJunk6 for the "Next Month" figures.
5 Create a final table to use as the basis for my column chart by union-ing tblJunk2 / tblJunk4 / tblJunk6
tblChartBase = UNION(tblJunk2, tblJunk4, tblJunk6)
Then it's good news / bad news, because although this seems to me to be a horrible hack solution, I do get the table I need with the correct figures and the correct shape, and I can base a column chart on it which gives me the analysis I am looking for. (Good News!)
However, once I add slicers to the page based on "Gender" and "Age Group", the measures in this new table do not respond to them and the figures do not change meaning that this is not the way to achieve what I want. (Bad News)
I am not sure whether I am kind of hedging around the correct methodology but need to tweak my DAX for the measures so they do respond to the slicers on the page, or whether this is totally the wrong approach from the ground up?
Any suggestions / pointers gratefully received.

I would address this requirement in the Query Editor. It has Unpivot functionality that should meet your needs, e.g.
https://support.office.com/en-us/article/unpivot-columns-power-query-0f7bad4b-9ea1-49c1-9d95-f588221c7098
The Query Editor has added a few more options since that article was written, like Unpivot Other Columns, Unpivot Only Selected Columns.

Related

PowerBI/DAX Measure as category for other measures

I have multiple simple calculated measures, which I would like to combine into categories, so that one could use this "category measure" in matrix visual.
Each measure basically counts data with some filter(s), like:
Blue =
CALCULATE(
COUNT(data[Full name]),
FILTER(data, data[White/Blue] = "Blue")
)
My attempt to this, was to create a table for measures categories, and then measure indexed accordingly.
Table:
Measures categories =
DATATABLE (
"Category", STRING,
"Sub-Category", STRING,
"Index", INTEGER,
{
{ "Direct/Indirect", "Direct", 1},
{ "Direct/Indirect", "Indirect", 2},
{ "White/Blue", "White", 3},
{ "White/Blue", "Blue", 4}
}
)
Measure:
Categories measure =
VAR SelectedMeasure_ =
SELECTEDVALUE('Measures categories'[Index])
RETURN
IF(HASONEVALUE('Measures categories'[Index]),
SWITCH(
SelectedMeasure_,
1, [Direct],
2, [Indirect],
3, [White],
4, [Blue]
)
)
This seems to work fine, however I'm missing Totals in Matrix visual, how do I get sum of measures per category/subcategory and total? I can workaround this by adding additional Total fields with corresponding sums, but there must be a better way.
One possible solution, is to create another measure, which sums value of Categories measure.
Your hasonevalue() looks like the problem. You need to rethink your approach to use values() but hard to advise further without a .pbix or full sample data to work from.

How to build a bar chart in Power BI from a questionnaire

My scenario is like that:
I have a table containing answers from a survey with two questions;
Each question has only three possible options: "yes", "no", "n/a".
What I need is a bar chart that shows how many questions with "yes" I have for each question. The final chart should have two colunms, one labeled "Q01" and other "Q02" (those are the names of the columns), each column counting the "yes" for the corresponding question.
That should be simple with filters and a clustered bar chart. Set the two columns (Q01 and Q02) to Y axis and one filter to each column to select only "yes". However, when I set the two columns together Power BI displais both columns with the same size, with what looks like the intersection of the both columns.
How to fix it?
Sample data
This is a simple example:
Generated from Sample = {(1, "yes", "yes"),(2, "no", "yes"),(3, "no", "yes"),(4, "yes", "no"),(5, "yes", "yes")}
The result is
Import your data into power query.
Select id columns and then unpivot other columns from the ribbon.
Close and apply.
Add a measure:
Measure = CALCULATE(COUNTROWS('Table'), 'Table'[Value] = "yes")
Add a stacked bar and add the following to the field well.

Is there a way to rename the categories of the legend?

I'm looking for a way to change the name of the legend of the graph without changing the data?
Unfortunately, you can't do this directly in the format page. You'll either have to create another column that contains a user-friendly name for your data. Then use that as the legend.
Ex.
Legend = SWITCH(
TRUE,
Table[Lag Days] <= 3, "Low",
Table[Lag Days] <= 7, "Medium",
Table[Lag Days] <= 30, "High",
"Extreme"
)
Or maybe use grouping to create the legend names that you would like.

How to make column on one sheet only contain values, or combinations of said values, present in another column?

I have a Google Sheets column where the only possible values should be combinations of values from another column. So in this image, the cell with product 4 would be rejected since it is not from column C
Normally, I would just use the data validation feature, but combinations of values such as "product 1, product 2" would also not be allowed since they are not "product 1" or "product 2" exactly and data validation only allows exact values from other columns.
I tried using a custom formula with Regexmatch, but I haven't managed to work it out as to only allow values from the specified column and had to resort to hardcoding it in, as such
=regexmatch(A1,"product 1"|"product 2"|"product 3")
but when I add a new value to the data column, I have to modify the formula again so it is not an optimal solution.
How Do I go about doing this now because I haven't found much info online?
you can play this out like:
=ARRAYFORMULA(REGEXREPLACE(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(IFERROR(SPLIT(A2:A, ","))<>"",
IF(REGEXMATCH(IFERROR(SPLIT(A2:A, ",")), TEXTJOIN("|", 1, C2:C)),
REGEXEXTRACT(IFERROR(SPLIT(A2:A, ",")), TEXTJOIN("|", 1, C2:C))&",", "♦"), ))
,,999^99)), "where not Col1 contains '♦'", 0), ",$|, $", ))

Power BI: How to pull in a column to an IF statement

I am trying to create a measure that says (basically) if the value in a column doesn't exists, display "No", otherwise display the column. Currently, I can't get this to pull in the column. This is essentially what I'm trying to accomplish
Status = IF(Table[Column] = BLANK(), "No", Table[Column])
Currently, the only thing I can get to work is this:
Status = IF(CALCULATE(COUNTA(Table[Column])) = BLANK(), "No", "Yes")