Calculated columns as axis & value - powerbi

I have a bunch of calculated DAX columns that I want to show as a visual. If I use a normal bar chart I get the following image, Barchart 1, where because I do not have any fields in the axis field. The titles of each of the calculated columns are what I want the x-axis to be similar to how it is in the funnel chart below.
The funnel chart only requires the value field to be filled in and it creates the following image which is kind of what I want but it needs to be vertical similar to the last image.
This final image, Barchart 3 is what I want to achieve with my calculated columns but so far I have had no luck in figuring this out. This visual was created using a different file which is irrelevant to the project I am working on now. I believe that if I could unpivot the calculated columns then it would create the graph I am looking for but I can't figure out how to unpivot columns that are created in DAX. Is there a way to unpivot DAX columns or a visual on the marketplace to accomplish what I am trying to do? Or would I need to create my own custom visual to accomplish this? Other ideas/thoughts?
Sample data file

I'd recommend creating a calculated table that has Month unpivoted so that you only need to put a single series on the bar chart.
For example, you can write a calculated table like this with only 7 columns:
CalcTable =
VAR ThisYear = YEAR ( MAX ( Sheet4[Start] ) )
RETURN
ADDCOLUMNS (
CROSSJOIN (
SELECTCOLUMNS (
Sheet4,
"Project", Sheet4[Project],
"Start", Sheet4[Start],
"End", Sheet4[End],
"Cost", Sheet4[Cost]
),
ADDCOLUMNS (
GENERATESERIES ( 1, 12 ),
"Month", FORMAT ( DATE ( ThisYear, [Value], 1 ), "MMMM YYYY" )
)
),
"MonthCost", IF (
[Value] >= MONTH ( [Start] ) && [Value] <= MONTH ( [End] ),
DIVIDE ( [Cost], DATEDIFF ( [Start], [End], MONTH ) + 1 ),
0
)
)
This table looks like this:
And allows you to create a bar chart with Month on the axis and sum of MonthCost for the values.

I ended up finding a solution to this. It doesn't maintain relationships but it works.
Totals Table =
UNION(
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[January 2020]),"Month","January 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[February 2020]), "Month", "February 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[March 2020]),"Month","March 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[April 2020]), "Month", "April 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[May 2020]),"Month","May 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[June 2020]), "Month", "June 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[July 2020]),"Month","July 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[August 2020]), "Month", "August 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[September 2020]),"Month","September 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[October 2020]), "Month", "October 2020"),
SUMMARIZE(Sheet4,"Cost",SUM(Sheet4[November 2020]),"Month","November 2020"),
SUMMARIZE(Sheet4, "Cost", SUM(Sheet4[December 2020]), "Month", "December 2020"))

Related

DAX Measure (e.g., "Count Rows") based upon filtered measure value (e.g., "[Sales]>100")

I need to write a DAX measure that calculates (e.g., "Count Rows"), but only when another measure value is evaluated (e.g., filtering "[Sales]>100"). So if-- in the context of the selected filters-- Sales is great than 100, then the measure is executed only for those rows.
The measure I have defined works in the context of lower smaller grain. But the totals do not sum correctly.
Any suggestions?
DAX Measure
License Usage =
// Users with active viewership in 3 months
IF (
NOT ( ISBLANK (
CALCULATE (
[Activity Date NEWEST],
KEEPFILTERS ( DATESINPERIOD ( dimCalendar[Date], TODAY (), -90, DAY ) )
)
)), 1
)
Activity Date NEWEST =
MAX('PBI Activity'[Date])
Okay, I figured something out that works.
DAX
License Usage =
// Users with active viewership in 3 months
CALCULATE (
[Count Users],
FILTER ( 'PBI Activity', 'PBI Activity'[Date] >= TODAY () - 90 )
)
Count Users = COUNTROWS('Users')
Also, I later came across this article which looks like it also does what I was hoping to do: Execute calculate expression over filtered rows based upon measure filter.
Reference: Specifying multiple filter conditions in CALCULATE - SQLBI
DAX
DEFINE
MEASURE Sales[Big Sales Amount] =
CALCULATE (
[Sales Amount],
KEEPFILTERS (
FILTER (
ALL ( Sales[Quantity], Sales[Net Price] ),
Sales[Quantity] * Sales[Net Price] > 1000
)
)
)
EVALUATE
SUMMARIZECOLUMNS (
Sales[Quantity],
"Sales Amount", [Sales Amount],
"Big Sales Amount", [Big Sales Amount]
)

Conditional Filtering on Report Data in Power BI

I have a table entry for multiple Product and Many different features. All the data has date time in shown format.
As we can see that all products has entries across different time. My aim is to filter the details related to the product by latest time of everyday.
I was able to split the Time col in Date col & Time col separately.
You can achieve that by creating a new table in Dax for eg. (because I don't know what do you want to do with rows with the same timestamp I show both rows for C)
YourFilteredTable =
VAR __trt =
TREATAS (
SELECTCOLUMNS (
ADDCOLUMNS (
SUMMARIZE (
ADDCOLUMNS (
ALL ( YourTable[Product], YourTable[Time] ),
"day", DATEVALUE ( YourTable[Time] )
),
YourTable[Product],
[day]
),
"MaxDate", CALCULATE ( MAX ( YourTable[Time] ) )
),
"Prod", [Product],
"MaxDate", [MaxDate]
),
YourTable[Product],
YourTable[Time]
)
RETURN
SUMMARIZECOLUMNS (
YourTable[Product],
YourTable[Quant],
YourTable[Time],
__trt
)

Create table with summarization and latest value of the year

For each ClientNo I want the Type classification corresponding to the last date of each Year:
Thus, the table above should be summarized as:
So, somehow, we need two intermediate tables:
Unique values for years, like VALUES(Table[Date].Year)
Unique values for ClientNo, like VALUES(Table[ClientNo])
Then for each combination of Year and ClientNo, get the latest date for each year and finally get the Type classification.
You should be able to do this in two steps along these lines:
Summary =
VAR MaxDates =
SUMMARIZE (
ADDCOLUMNS ( Table1, "Year", YEAR ( Table1[Date] ) ),
Table1[ClientNo],
[Year],
"MaxDate", MAX ( Table1[Date] )
)
RETURN
SELECTCOLUMNS (
MaxDates,
"ClientNo", [ClientNo],
"Year", [Year],
"Type", LOOKUPVALUE (
Table1[Type],
Table1[ClientNo], [ClientNo],
Table1[Date], [MaxDate]
)
)
In calculating the variable, we add a Year column and then calculate the maximal date corresponding to that year.
Then we take that table variable, pick out the ClientNo and Year columns, and look up what the Type corresponding to the MaxDate.
If Note: you want to keep the MaxDate column, replace
[...] SELECTCOLUMNS ( MaxDates, "ClientNo", [ClientNo], "Year", [Year], [...]
with
[...] ADDCOLUMNS ( MaxDates, [...]

DAX moving average not working

I try to calculate moving average in DAX power bi. I use different codes, for example this.
Moving AverageX 7B Days =
AVERAGEX (
DATESINPERIOD(
sahkoInput[Date];
LASTDATE ( sahkoInput[Date]);
-7;
DAY
);
sahkoInput[price]
)
All codes give the same result - Moving AverageX 7B Days is equal to column "price". What went wrong and how to fix it?
Firstly, I would look to add a date/calendar table to your data model.
This can be as simple as a list of consecutive dates from at least seven days before your first data point until after the end of when you expect your last one to be.
The reason for this is that the date functions in DAX always work best when they have a table of consecutive dates to look at - you can get unpredictable results when your fact table doesn't have any data on a particular date.
Once you have added the date table, create a relationship to link the date column in your sahkoInput table to the date column in your date table.
Now, the following measure should work:
Moving AverageX 7B Days =
CALCULATE (
AVERAGE('sahkoInput'[Price]);
DATESINPERIOD ('DateTable'[Date];
LASTDATE ('DateTable'[Date]);
-7;
DAY)
)
Create a date table, this will dramatically improve performance and increase readability. You can do that by using the following DAX:
Min Date := MIN('sahkoInput'[Date])
Max Date := MAX('sahkoInput'[Date])
Dates :=
VAR BaseCalendar =
CALENDAR ( [Min Date], [Max date] )
RETURN
GENERATE (
BaseCalendar,
VAR BaseDate = [Date]
VAR YearDate =
YEAR ( BaseDate )
VAR MonthNumber =
MONTH ( BaseDate )
RETURN
ROW (
"Day", BaseDate,
"Year", YearDate,
"Month Number", MonthNumber,
"Month", FORMAT ( BaseDate, "mmmm" ),
"Year Month", FORMAT ( BaseDate, "mmm yy" )
)
)
Then referencing this date table you can create an average using the standard average function, like so:
Moving Average 7 Days :=
CALCULATE (
AVERAGE ( 'sahkoInput'[Price] );
KEEPFILTERS ( DATESINPERIOD ( 'Dates'[Date]; MAX ( 'Dates'[Date] ); -7; DAY ) )
)
I hope this helps!

Dynamic DAX Number Format

I want to dynamically change the number format of a DAX measure, based on a dimension value (or indeed, based on the order of magnitude of the measure value).
I understand I can use SWITCH and FORMAT, as demonstrated by Kaspar De Jonge here: https://www.kasperonbi.com/dynamic-format-using-dax/
Here's an example of the type of measure I'm creating:
My Measure:=IF (
HASONEVALUE ( dimMeasureType[Measure Type] ),
SWITCH ( VALUES ( dimMeasureType[Measure Type] ),
"Total Cost", FORMAT ( [Total Cost], "#,##0, k" ),
"Cost Per Unit", FORMAT ( [Cost Per Unit], "#,##0.00" ),
"Cost % Sales", FORMAT ( [Cost % Sales], "0.00%" ),
BLANK()
),
BLANK()
)
But this technique returns text measures. I need to be able to chart my measures, so I do not want to convert them to text. Is there another technique for dynamically changing a measure number format, without converting to a string?
If it makes a difference, I'm working in SSAS-Tabular on SQL Server 2016 BI.
I don't believe this is currently possible, but it a popular feature request that will hopefully be implemented in the future.
I recommend voting and commenting on the idea I linked to in order to add your support.
A workaround is to create multiple measures and add them all to your chart. Depending on your dimension value only one measure returns values, all other measures return BLANK() and are not displayed in your chart. You can give them the same display name by adding whitespace to the end of their names:
My Measure:=IF (
SELECTEDVALUE( dimMeasureType[Measure Type] ) = "Total Cost",
[Total Cost],
BLANK()
)
[My Measure ]:=IF (
SELECTEDVALUE( dimMeasureType[Measure Type] ) = "Cost Per Unit",
[Cost Per Unit],
BLANK()
)
[My Measure ]:=IF (
SELECTEDVALUE( dimMeasureType[Measure Type] ) = "Cost % Sales",
[Cost % Sales],
BLANK()
)
This has some drawbacks though:
The chart legend shows all measures, even if all their values are BLANK().
The y-Axis of your chart has the same format as the first measure in its 'Values' section.