使用Power BI上的Slicer选项在折线图中切换测量

时间:2019-09-11 06:51:25

标签: powerbi dax

我有一个折线图,显示了过去三年的年初至今收入,还有一个目标线。

我有3个切片机的Region,District和Branch都来自表格DIMBRANCH

我有以下目标表:

goalbyRegion

goalbyBranch

goalbyCompany

要求:如果未在切片器上选择任何内容,则应该显示表goalbycompany中的目标。 选择区域后,在goalbyregion表中输入的所选区域的折线图上显示目标。

问题当前,收入因切片器选择而异,因为收入来自单个表tbl_revenue。

enter image description here

如果未在切片器上选择任何内容,则该图像显示了收入和目标折线图。

当前使用的DAX:

2017/Revenue = 
VAR _year =
    YEAR ( TODAY () ) - 2
RETURN
    CALCULATE (
        SUM ( Revenue[Revenue] ),
        FILTER ( Revenue,Revenue[Year] = _year )
    )


2018/Revenue = 
VAR _year =
    YEAR ( TODAY () ) - 1
RETURN
    CALCULATE (
        SUM ( Revenue[Revenue] ),
        FILTER ( Revenue,Revenue[Year] = _year )
    )



2019/Revenue = 
VAR _year =
    YEAR ( TODAY () ) 
RETURN
    CALCULATE (
        SUM ( Revenue[Revenue] ),
        FILTER ( Revenue,Revenue[Year] = _year )
    )


2019/Goals Amount = 
VAR _year =
    YEAR ( TODAY () ) 
RETURN
    CALCULATE (
        SUM ( GoalByCompany[GoalAmount] ),
        FILTER ( GoalByCompany, GoalByCompany[Year] = _year )
    )

DAX我正试图通过切换功能实现我的目标。

Switch Goal = 
var _region = SELECTEDVALUE(Branch[Region])
var _branch = SELECTEDVALUE(Branch[Branch])
var _district = SELECTEDVALUE(Branch[District])

var _year = year(TODAY())
var _goalregion = CALCULATE(
                    SUM(GoalByRegion[GoalAmount]),
                    FILTER(GoalByRegion , GoalByRegion[year] = _year))

var _regionselection = 
    SWITCH(_region,
        "RegionGoal",_goalregion
    )

return 
switch (_regionselection,"Region",_goalregion,blank())

我还不习惯切换功能。我以为可以满足我的要求。

enter image description here

这是我选择区域的时间。

这是我的表之间的关系

附加表:收入表链接到带有branchID的分支表 calendarweek表通过weekingingdate列链接到日期表。

enter image description here

1 个答案:

答案 0 :(得分:1)

如果仅考虑是否对区域进行过滤,则无需使用SWITCH。您可以简单地使用IF语句来确定要计算的度量:

2019/Goals Amount = 
VAR _year = 
    YEAR ( TODAY() )

RETURN
    IF ( 
        HASONEFILTER ( Branch[Region] ),
        CALCULATE (
            SUM ( GoalByRegion[GoalAmount] ),
            FILTER ( GoalByRegion, GoalByRegion[Year] = _year )
        ),
        CALCULATE (
            SUM ( GoalByCompany[GoalAmount] ),
            FILTER ( GoalByCompany, GoalByCompany[Year] = _year )
        )
    )