【问题标题】:Get total sum for each month in DAX在 DAX 中获取每个月的总和
【发布时间】:2016-04-27 17:55:51
【问题描述】:

我有一个连接到 sql server 数据库的表格模式分析服务器。我想得到每个月的总 x,而我每天有总 x。因此,例如,我有一个表 DailyEvent,其中前 2 列是这样的,我想要列“MonthXCount”:

TimeID   XCount MonthXCount
20160429  3          11
20160430  8          11
20160501  4           4

所以 4 月的总 XCount 是 11,所以对于 4 月的每一天,我想要 11。5 月的总 X 计数是 4(到目前为止)。

我现在拥有的是我认为的 MonthToDate 总数,计算如下:

=TOTALMTD(SUM('DailyEvent'[XCount]),'DailyEvent'[TimeID])

但我想有一个列,将月份的最后一个值放入该月的每一天。

最终目标是在 PowerBI 中按月绘制 XCount 图表,因此我可能不需要在此处添加列...我也许可以告诉 PowerBi 绘制该月的最后一天,但我'我不知道该怎么做,并认为这会更容易。

【问题讨论】:

  • 绝对是一个 mdx 问题吗?还是应该是 dax?
  • 这绝对是 DAX 问题。

标签: sql-server dax ssas-tabular


【解决方案1】:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = "begin transaction t    
    update table dailyevent
    set monthxcount = (select top(1) monthxcount 
                            from dailyevent 
                            where MONTH(timeID) = 4 
                            order by monthxcount desc)
    where MONTH(timeID) = 4

    --commit only if the rows affected corresponds to the number of rows found for the month of april.

    commit";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.

sqlConnection1.Close();

【讨论】:

  • 非常感谢,但我不知道该放在哪里?我正在使用 Visual Studio 并查看 Model.bim 文件。我只是在 fx 框中输入了上面的等式作为新列。我是新手,不知道如何应用您的建议。
  • 您在 VStudio 中使用的是什么语言? SqlConnection sqlConnection1 = new SqlConnection("你的连接字符串"); SqlCommand cmd = 新的 SqlCommand(); SqlDataReader 阅读器; cmd.CommandText = "SELECT * FROM Customers"; cmd.CommandType = CommandType.Text; cmd.Connection = sqlConnection1; sqlConnection1.Open();阅读器 = cmd.ExecuteReader(); // 数据可通过此处的 DataReader 对象访问。 sqlConnection1.Close();
【解决方案2】:

您的模型中有一个日历表连接到您的数据表吗?

如果是,假设日历表名为 DimDate,并且它包含名为 YearMonth 的列,那么公式为:

Month Sales := SUMX(
                    VALUES(DimDate[YearMonth]),
                    SUM(DailyEvent[XCount])
                    )

如果您没有日历表,则可以使用以下公式在名为 YearMonth 的表中创建一个计算列:

=LEFT(DailyEvent[TimeID], 6)

然后计算月销售额的总和:

Month Sales := SUMX(
                    VALUES(DailyEvent[YearMonth]),
                    SUM(DailyEvent[XCount])
                    )

希望这会有所帮助!

关于您使用的公式的说明: 时间智能功能,例如 TOTALMTD,需要日历表。因此,请务必在使用它们之前将其添加到您的数据模型中。

编辑:

另一种解决方案可能是创建一个日期列:

     = DATE(
            LEFT(DailyEvent[TimeID], 4), 
            MID(DailyEvent[TimeID], 5, 2), 
            1)

然后将该列放在图表的 X 轴上,并将 XCount 列放在 Y 轴上。

【讨论】:

    【解决方案3】:

    您可以在 Power BI 或 SSAS 模型中使用度量计算,如果有日历表,请确保将其标记为日期表,否则时间逻辑将不起作用。我使用以下查询 (DAX) 它计算本月至今

    MtD :=CALCULATE(SUM('DailyEvent'[XCount]),DATESMTD('DateTabe'[calendarDate]))
    

    但是,如果没有日历或者您正在使用定制日历,这可能会有所帮助 Running Totals Whitout a Traditional Calendar Table

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 2021-02-18
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      相关资源
      最近更新 更多