What I'm trying to do is to make a query formula returning year sales by week number filtered by store.
Example sheet: Link
It's easy to do with formula like this one: =Query(query(A:D,"Select A,Sum(D) where A is not null group by A Pivot B",1),"Select * offset 1",0)
But I also need to filter results based on specific store (Col C)
It's also not hard:
=Query(query(A:D,"Select A,Sum(D) where C = 'First' AND A is not null group by A Pivot B",1),"Select * offset 1",0)
But in this case any week with 0 sales and store equals to 'Second' will be missed.
I would like to show all weeks (Col A) presented in the data. Is it possible?
try:
=ARRAYFORMULA(QUERY(QUERY({A:B, IF(C:C<>"First", {"First", 0}, C:D)},
"select Col1,sum(Col4)
where Col3 = 'First'
and Col1 is not null
group by Col1
pivot Col2"), "offset 1", 0))
Related
Hi i have an issue i cant fix in PowerBI i dont understand DAX that mutch.
I have a half solution in DAX and an example what i have tryed.
I have the solution in SQL.
WANTED RESULT
I need to get the "time" result summed up
that have values between the two selected values.
IMPORTANT if one rows of values starts before AND after the selected values then the operation was active that time and shall be included.
#sdate = '2020'
#sdate = '2021'
Select *
From #temp
where (datepart(year,startdate) <= #sdate and datepart(year,enddate))
or (startdate between #sdate and #edate)
or (enddate between #sdate and #edate)
If i do it in SSMS i get the right rows
But in PowerBI i have some issue
I need to be able to choose två year
Year from startdate
Year from enddate
This part work but not the full solution
I just get row 3 and 4 as it sould of this solution.
UPDATED
NOW I DONT EVEN GET THIS PART RIGHT
I want all the green to include and exclude the red
Antal (under året) =
var SelectedYearStart = CONVERT(SELECTEDVALUE(TEST[startdate].[Year]), INTEGER)
var SelectedYearEnd = CONVERT(SELECTEDVALUE(TEST[enddate].[Year]), INTEGER)
return CALCULATE(SUM(TEST[time]),ALLCROSSFILTERED(TEST),year(TEST[startdate])<=SelectedYearStart , year(TEST[enddate])>=SelectedYearEnd)
My guess was this
Antal (under året) =
var SelectedYearStart = CONVERT(SELECTEDVALUE(TEST[startdate].[Year]), INTEGER)
var SelectedYearEnd = CONVERT(SELECTEDVALUE(TEST[enddate].[Year]), INTEGER)
return CALCULATE(SUM(TEST[time]),ALLCROSSFILTERED(TEST),year(TEST[startdate])<=SelectedYearStart , year(TEST[enddate])>=SelectedYearEnd || DATESBETWEEN(TEST[startdate],SelectedYearStart,SelectedYearEnd || DATESBETWEEN(TEST[enddate],SelectedYearStart,SelectedYearEnd)))
But then i get this error
TEST DATA
declare #sdate nvarchar(4)
declare #edate nvarchar(4)
set #sdate = '2020'
set #edate = '2021'
select #sdate sdate
select #edate edate
DEclare #temp table (time decimal(18,2) , startdate date, enddate date)
INSERT INTO #temp
SELECT 5.0,'2019-01-01','2020-12-01' union all --
SELECT 5.0,'2021-01-01','2022-12-01' union all --
select 5.0,'2020-01-01','2021-12-01' union all
select 5.0,'2019-01-01','2022-12-01' union all --
select 5.0,'2019-01-01','2019-12-01' union all --
select 5.0,'2022-01-01','2022-12-01' union all
select 5.0,'2020-01-01','2020-12-01' union all
select 5.0,'2021-01-01','2021-12-01'
--select 5.0,'2020-01-01','3000-01-01' --EXTRA
SELECT *
into TEST
FROM #temp
--ORDER BY startdate,enddate
You need something like this:
var SelectedYearStart = SELECTEDVALUE(TEST[startdate].[Year])
var SelectedYearEnd = SELECTEDVALUE(TEST[enddate].[Year])
return CALCULATE(SUM(TEST[time]),
ALL ( TEST[startdate].[Year]),
ALL ( TEST[enddate].[Year]),
KEEPFILTERS(
(Year(TEST[startdate]) <= SelectedYearStart &&
Year(TEST[enddate]) >= SelectedYearEnd)
|| (Year(TEST[startdate]) >= SelectedYearStart &&
Year(TEST[startdate]) <= SelectedYearEnd)
|| (Year(TEST[enddate]) >= SelectedYearStart &&
Year(TEST[enddate]) <= SelectedYearEnd))
)
First you need to clear the filters on the table that are created via the two slicers (startdate.Year and enddate.Year)
Then you need to pass in your complex filter query. Note that I'm using the Year(..) function because you can't reference columns from different tables in the filter section of the Calculate (and startdate.year and enddate.year are coming from two separate date tables created automatically by powerbi).
Finally you need to wrap that into a KEEPFILTERS statement to ensure that only the current row context is being applied to the expression.
I have a dataset grouped by two rows as below
GroupName Score Multiply
Group1 2
Group2 3
Group2 5
Group1 1
I have a slicer based on the parameter table for storing the above GroupName values. so when I select Group1( I am using a selected function on variable in my dax) I want to multiply all rows for Group1(scores) by 4 and all rows for Group2(score) by 6.
I tried this but it is updating all rows.
var a=4*score
var b=6*score
var mm=selectedvalue('Group Paramter'[Group Name])
Return if( mm ="Group1", a, b)
But it multiplies all rows, how can I multiply by grouping using GroupName?
How can I achieve this? I apperciate for any help.
You can try some logic as below (measure)-
var mm = selectedvalue('Group Paramter'[Group Name])
Return if( mm ="Group1", min(table_name[score]) * 4, min(table_name[score]) * 6)
Link: https://learn.microsoft.com/en-gb/learn/modules/dax-power-bi-modify-filter/5-context-transition
When a unique column is on the table, you only need to apply a filter
on that column to make the transition happen. In this case, Power BI
applies a filter on the CustomerKey column for the value in row
context.
I am looking for an example to understand this.
Consider the following tables with and without a unique ID column.
Table1
ID
ABC
XYZ
Value
1
a
x
5
2
a
x
2
3
a
y
2
4
b
y
2
5
b
z
3
Table2
ABC
XYZ
Value
a
x
5
a
x
2
a
y
2
b
y
2
b
z
3
Let's define measures
SumValue1 = SUM ( Table1[Value] )
SumValue2 = SUM ( Table2[Value] )
If you calculate each respective measure within the row context (e.g. using a measure in a calculated column) of the first row on each table, then the context transition will turn the row context into a filter context that looks like this for the first table
CALCULATE (
[SumValue1],
FILTER ( Table1, Table1[ID] = 1 )
)
but like this for the second table
CALCULATE (
[SumValue2],
FILTER (
Table2,
Table2[ABC] = "a"
&& Table2[XYZ] = "x"
&& Table2[Value] = 5
)
)
Because there is no unique column in the second case, it's necessary to look at all of the columns to know which row we're referring to.
When there is a unique ID column, you can shortcut this since there is a one-to-one correspondence between rows and ID values, so that specifying the ID is equivalent to specifying the row and vice versa.
For ease of understanding, the table has 3 columns: (Date, Item, Value). I want to add a conditional aspect to my calculate-sum measure to only sum when Item 1 is present. Right now Item 3 is a row that is sometimes present by itself but I don't want that as it's a false-positive for my data set.
Current:
Measure = CALCULATE(SUM('Table'[Values]),
FILTER('Table',
'Table'[Item] = "Item 1" ||
'Table'[Item] = "Item 2" ||
'Table'[Item] = "Item 3"
))
You can create a calculated column, to make the value 0 for all other cases except Item1:
Column = IF(Table[Item]="C",Table[value],0)
Now you can sum up this column to get the results you are looking for.
Edit based on Comment:
Check_Column =
VAR Step1=CALCULATE(MAX('Table'[Item]),CALCULATETABLE('Table',ALLEXCEPT('Table','Table'[Month]),'Table'[Item]<>"C"))
VAR Step2 = IF(Step1=BLANK(),0,'Table'[Value])
Return Step2
The above calculation will not count any record which has only a value of C.You can simply sum up the column to get the result you are looking for.
I have two dates:
'2018-01-05' and '2019-01-05'
How to create calculated table to break down those dates by month.
Should look simething like that:
There are probably many ways to do this, but here's one way that combines a few different concepts:
Table =
VAR Starting = DATE(2018, 1, 5)
VAR Ending = DATE(2019, 1, 5)
VAR MonthTable =
SUMMARIZE(
ADDCOLUMNS(
CALENDAR(Starting, Ending),
"StartDate", EOMONTH([Date], 0) + 1),
[StartDate],
"EndDate", EOMONTH([StartDate], 0) + 1)
RETURN UNION(
ROW("StartDate", Starting, "EndDate", EOMONTH(Starting, 0) + 1),
FILTER(MonthTable, [EndDate] < Ending && [StartDate] > Starting),
ROW("StartDate", EOMONTH(Ending, -1) + 1, "EndDate", Ending)
)
Basically, you start with the CALENDAR function to get all the days, tag each date with its corresponding month, and then summarize that table to just return one row for each month.
Since the first and last rows are a bit irregular, I prepended and appending those to a filtered version of the summarized month table to get your desired table.
Create new table as
Table = CALENDAR( DATE(2018, 5, 1), DATE(2019, 1, 5) - 1)
Rename auto-generated column "Date" into "Start Date". Add new column as
End Date = Start Date + 1