How to subtract values from a same column in DAX on the basis of other selected values from a different column? - powerbi

So I habe data like this.
Date. Type Value
19/1/2020. A. 12
19/1/2020. B. 20
..
20/1/2020. A. 40
..
20/1/2020. B. 20
.
.
.
I want to calculate A - B and plot it on a graph. I can't write Type = A in a formula to filter as I have many types. But i just want to subtract only two types at once, not more.
any help will highly be appreciated

You have to pivot your table like below:
Note that I have named the table as "Types" and your new column as "Difference"
Then you can create a calculated column like below:

Related

DAX & Power Query - Removing transaction line if sale is returned

First time posting, thanks in advance !
I have a simple sales table acting as a data source.
In this table I have all my dimensions and facts, the data model is not organized as a star schema.
I believe this question does not require the data model to be optimally built.
One column tracks the Sales Quantity and shows -1 if the item is returned.
In Power Query.
I wish, for all returns, to eliminate the line with the -1, but also the associated line with the +1 sale.
I do not wish to simply filter by the Invoice Number and remove it, as in the same invoice I may have other items who were not returned.
I wish to have in my final table only final records.
Thank you
Did not manage to identify a way to tackle the issue in Power Query as of yet.
Edit, addition of further context
Dataset abstract:
I would like to keep the green row and remove the yellow ones.
One invoice features three lines, two for one item being sold and returned, and one for an item being purchased for good. That latter one is to stay in the records.
[IMAGE : Excel version of relevant column in data base ]
https://i.stack.imgur.com/CiiiC.png
Because I do not know the exact nature of your data, I will give you a general solution for your problem, which will help you immensely.
First, I will transform your data into facts and questions. Then I will answer the questions depending on my experience analyzing many different datasets; last but not least, I will provide you with the solution.
Facts,
[Sales Amount Include Tax] column values for Return Sales are negative numbers because excel format negative numbers by including them with parentheses, as your data shows.
The Original Sale record and the Return Sale record must have the same values for these columns, which we will use to know the Original Sales record:
The absolute value for the [Sales Amount Include Tax] column.
[Invoice No], [Department], [Sub Dept], [Sub Dept GROUP], [Item Code] columns.
This point is critical because we will use these columns to know the Original Sales record.
Questions,
Is the Item code column value similar to the original sale row and the returned sales row?
My answer: It should be because you are returning the same item that the Item code column describes, but I am concerned that your data shows that the first two rows don't have the same Item code. Is it by mistake?
Is the Sales Quantity column always filled with -1, even if the original sales quantity was, for example, three pairs of shoes?
My answer: The Sales Quantity column should describe the number of items someone bought, so if I originally bought 2 things but didn't like them for a reason, I will return two items so that the column value will be -2.
If my above assumptions are correct, you need to do the following in the Power BI query:
1. Import your Sales table to Power BI. See this image that shows the test data I used.
2. When you import your data and see it in the Power Query Editor, you will see that the Sales Amount Include Tax column value shows minus rather than parentheses for Return Sales Rows, as you can see in this image.
3. Create a copy of your Sales table and Rename it to Returned Sales, then filter this table to include only [Sales Amount Include Tax] that are less than 0, as you can see in this image.
4. Filter the Sales table to include only [Sales Amount Include Tax] greater than or equal to 0, as you can see in this image.
P.S. Now comes the fun part :)
1. In Returned Sales table, add a custom column name it ReturnedSaleFlag, then give it a value of 1, as you can see in this image.
2. Change ReturnedSaleFlag column type to Whole number.
3. In Returned Sales table, transform [Sales Amount Include Tax] to an Absolute Value, as you can see in this image.
4. Now you need to return to the Sales table and merge it with Returned Sales table using multiple columns which are the column listed in the second point of the fact section, as the image shows. Also, see this link that describes how to Merge query based on multi columns.
5. Expanded the new Returned Sales column in Sales table and only select ReturnedSaleFlag column and make sure to remove the check from use original column name as a prefix, as the image shows.
6. Finally, all your previous hard work created a flag in your Sales table that will let you know the Sales record that had been returned. :)
7. Now you filter the Sales table to keep the records that only have null values for the new ReturnedSaleFlag column then remove this column, as the image shows.
8. In the end save and apply and analyze your Sales and your extra info in the Returned sales.
I hope I helped in a way; if so, please mark this as an answer and vote for it :)

PowerBi Join column value strings together in each row

I have a table like shown below titled Employees:
Name LOC1 LOC2 LOC3 LOC4
-----------------------------------------
Joe DAY CVG DTW
Jane PVD STL
John LAX SAN SFO ANC
I created a measure to combine the LOC fields into 1 result separated by a space. I keep getting errors because PBi wants to aggregate the data fields. All fields are type Text. I've tried all the usual combine methods, below are a couple of examples...
CombineLOCs=CONCATENATE('Employees'[LOC1],'Employees'[LOC2])
CombineLOCs= 'Employees'[LOC1] & " " & 'Employees"[LOC2]
This is what I get back:
"A single value for column 'LOC1' in table 'Employees' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation....etc."
I ultimately have a table visual on my canvas and instead of listing out all of the columns on that table, I just want 1 column with all of the combined LOC values for each employee in it.
If you just want a single column where the LOC columns are joined together, then you need to create a new column within the table, rather than a new measure. A measure expects you to aggregate the values. A column will concatenate the text strings on a row by row basis.
If you select the new column option, either on the table tools or the fields sidebar, then use the concatenate formula in your question, that should work.
With the new column, you can then include the employee name and the associated LOC values.

Create Table with a columns is a sum from values from other tables

I'm "bugging" to transform my datas to something usable.
For the moment, a datasource provides me a table where a column contains dates, following by 24 columns representing each hours. In this 24 columns, for each date (each row) I've a total of phone calls.
I want to show the hourly repartition. So, my original datasource is not really usable ans need to transform it with something where there is a column "hour" (a simple index from 0 to 23 or 1 to 24) and a column with the total call for each column from the original column. But I'm a lot confused to do it because I don't have a way to create a relationship. Like this :
Someone have any idea to help me? Thanks in advance
I would unpivot the data source and then create two dimensions (Calendar) and (Time).

Calculated column offset by 2

https://i.stack.imgur.com/BTjZv.jpg
I am trying to generate a calculated column B from column A. Notice that column B is basically the same rows as column A except it's offset by 2 rows. I'm trying to generate this with DAX but I don't even know where to begin. I would appreciate any help.
You can use in PowerBI the PowerQuery Editor to append a Column with 2 "null" values (as numeric type) like the example below:
append column in powerbi example
To get this result:
final result of "column B"
Think is a easy way to get what you want.

A table of multiple values was supplied where a single value was expected, when using Distinct

I have the following table:
I'm trying to create a new column using Dax language:
Column = DISTINCT('Edição'[País])
But I gott this error message:
A table of multiple values was supplied where a single value was expected.
The expected result is:
You have a source table with 21 rows. You want to add a column. In the end you'll still have 21 rows.
The "expected" table has 18 rows. So with only adding a column you will never get there, it's just impossible.
To get the expected output though, you can do this easily: just create a table with just Pais as values and PowerBI will automatically group. See here a (more minimal) example:
PS. If you wanted to add a column -say- with the number of years the country appears in the source data, you should:
choose the matrix visualization
add "Pais" (country) for rows
add e.g. "Ano" (year) for values, and summarize by "Count (Distinct)"