Split data into categories in the same row in Power BI - powerbi

I have a table that contains multiple columns with their named having either the suffix _EXPECTED or _ACTUAL. For example, I'm looking at my sold items from my SoldItems Table and I have the following columns: APPLES_EXPECTED, BANANAS_EXPECTED, KIWIS_EXPECTED, APPLES_ACTUAL, BANANAS_ACTUAL, KIWIS_ACTUAL (The Identifier of the table is the date, so we have results per date). I want to show that data in a table form, something like this (for a selected date in filters:
+------------+----------+--------+
| Sold items | Expected | Actual |
+------------+----------+--------+
| Apples | 10 | 15 |
| Bananas | 8 | 5 |
| Kiwis | 2 | 1 |
+------------+----------+--------+
How can I manage something like this in Power BI ? I tried playing with the matrix/table visualization, however, I can't figure out a way to merge all the expected and actual columns together.

It looks like the easiest option for you would be to mould the data a bit differently using Power query. You can UNPIVOT your data so that all the expected and actual values become rows instead of columns. For example take the following sample:
Date Apples_Expected Apples_Actual
1/1/2019 1 2
Once you unpivot this it will become:
Date Fruit Count
1/1/2019 Apples_Expected 1
1/1/2019 Apples_Actual 2
Once you unpivot, it should be fairly straightforward to get the view you are looking for. The following link should walk you through the steps to unpivot:
https://support.office.com/en-us/article/unpivot-columns-power-query-0f7bad4b-9ea1-49c1-9d95-f588221c7098
Hope this helps.

Related

power automate desktop - convert list into data table

I read some text from a pdf into Power automate desktop. It is in the form of a list like
0 | 123 Testing Company 23.00
1 | Generation Z Co 555.11
2 | Tea Company 1,234.99
I need to separate the list into columns where the number at the end of the element is in its own column like
0 | 123 Testing Company | 23.00
1 | Generation Z Co | 555.11
2 | Tea Company | 1,234.99
Is there a way to do this? I've tried to extract tables from PDF instead but this method does not return the right data, because it seems PAD doesn't recognize it as a table.
Is there a way to convert a list into a data table?
The only way seems to be to For Loop the List,
Parse Text each of the columns
and insert them each as a row to the Data table.
i feel like there should be a quicker way to do this.

Extract values from column to use in calculated measure formula

I have TABLE A
In that table I have a measure with values like so:
Targets|
--------
4 |
5 |
6 |
In the same table I have a calculated column (summed totals) like so:
Totals |
--------
10 |
11 |
12 |
Because this is a direct query data source, query editor is disabled and manipulation must be done through DAX formulas.
I would like to do a simple operation of Targets-Totals
Code I've tried for a calculated column:
test = TableA[targets] - TableA[totals]
However this results in an error:
The column TableA[test] cannot be pushed to the remote data source and cannot be used in this scenario.
How can I create a new column with the above operation considering the fact that one column is a 'measure ' and the other a 'calculated column'
In this case, you will need a measure that does a row by row calculation, but not as a calculated column. For this you will need SUMX which will do a iteration.
Your measure should be:
New Measure = SUMX(TableA, [targets] - [totals])

SQL Scalar Function values into power bi table

I have two SQL functions in Power BI
Each returns one number each (say fn_1 =3 fn_2 = 4). What I am trying to do is create a table that has 2 column 1 row with fn_1 as a colum and fn_2 as the 2nd column
|---------------------|------------------|
| fn_1 | fn_2 |
|---------------------|------------------|
| 3 | 4 |
|---------------------|------------------|
Tried duplicate, combine, aggregate, merge. They're all returning something different.
Thanks
EDIT: The only reason wanting to do that is so I can combine them into 1 row as 3/4 (which I can do fine with regular table) have both values (3 and 4 as 3/4) display on top of a report as 3/4 using a card.
For two values, I'd try some basic table constructions:
#table({"fn_1", "fn_2"}, {{fn_1, fn_2}})
or
Table.FromRecords({[fn_1 = fn_1, fn2 = fn_2]})
or
Table.FromColumns({{fn_1}, {fn_2}}, {"fn_1", "fn_2"})

Power BI - how to count duplicates from two identical sets with different filters

I have two tables which contain the same data - TableA (PersonId, ItemId, Session) and TableB(PersonId, ItemId, Session). Here is some test data:
PersonId | ItemId | Session
-------------------------------------------------
1 | 1 | Summer 2017
1 | 1 | Spring 2017
2 | 1 | Summer 2017
2 | 2 | Summer 2017
3 | 5 | Spring 2017
There are two slicers for the different sessions (for simplicity I have only showed two in the test data but they may be a lot more). The user has to see the number of different PersonIds for the selected session in the first slicer by
ItemId. Then there should be information on the number of PersonIds that are both present in the first and second sessions in the selected slicers.
My idea is to filter TableA by the selected value in the first slicer and then filter TableB by the selected value in the second slicer, but I am not sure how to do that dynamically? Also, I may need a third table for the intersection of filtered TableA and Filtered TableB. Do you have any ideas on how I achieve this functionality?

Google Chart API table format for charts

Hopefully this makes sense. I will probably just keep mulling this over, until i figure it. I have a table, that is formatted in such as way, that a specific date may have more than one record assigned. Each record is a plant, so the structure of that table looks like the pinkish table in the image below. However when using the Google chart API the data needs to be in the format in the blue table for a line chart. Which I have working.
I am looking to create a graph in the Google chart api similar to the excel graph, using the pink table. Where at one date e.g. 01/02/2003 there are three species recorded A,B,C with values 1,2,3. I thought possibly using a scatter but that didn't work either.
What ties these together is the CenterID all these records belong to XXX CenterID. Each record with its species has an SheetID that grouped them together for example SheetID = 23, all those species were recorded on the same date.
Looking for suggestions, whether google chart API or php amendments. My PHP is below (I will switch to json_encode eventually).
$sql = "SELECT * FROM userrecords";
$stmt = $conn->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll();
foreach ($data as $row)
{
$dateArray = explode('-', $row['eventdate']);
$year = $dateArray[0];
$month= $dateArray[1] - 1;
$day= $dateArray[2];
$dataArray[] = "[new Date ($year, $month, $day), {$row['scientificname']}, {$row['category_of_taxom']}]";
To get that chart, where the dates are the series instead of the axis values, you need to change the way you are pulling your data. Assuming your database is structured like the pink table, you need to pivot the data on the date column instead of the species column to create a structure like this:
| Species | 01/02/2003 | 01/03/2003 | 01/04/2003 |
|---------|------------|------------|------------|
| A | 1 | 2 | 3 |
| B | 3 | 1 | 4 |
| C | 1 | 3 | 5 |