[英]When importing data into Power BI from SQL Server, how do you dynamically change a value used in the SQL query?
I have the following query that imports data from SQL Server into Power BI.我有以下查询将数据从 SQL 服务器导入 Power BI。
SELECT *
FROM table_name
WHERE transaction_date > '2020-01-01 00:00:00'
I need to be able to change the value of the transaction_date
(a datetime value) without having to edit the query each time.我需要能够更改
transaction_date
的值(日期时间值),而不必每次都编辑查询。
I attempted to do this using a parameter but I do not know how to add it properly to the SQL query.我尝试使用参数来执行此操作,但我不知道如何将其正确添加到 SQL 查询中。 The parameter
current_date
is a datetime value.参数
current_date
是日期时间值。
SELECT *
FROM table_name
WHERE transaction_date > '"¤t_date&"'
I get the error:我收到错误:
Conversion failed when converting date and/or time from character string.
从字符串转换日期和/或时间时转换失败。
Please tell me how to use the parameters, or if there is any other way to do this.请告诉我如何使用这些参数,或者是否有任何其他方法可以做到这一点。
You can convert the value to text using appropriate date format accepted by your database, for example this way:您可以使用数据库接受的适当日期格式将值转换为文本,例如这样:
DateTime.ToText(current_date, [Format="yyyy-MM-dd HH:mm:ss", Culture="en-US"])
And then use that value in your M code to modify the query:然后在您的 M 代码中使用该值来修改查询:
let
Source = Sql.Database(".", "SomeDatabaseName", [Query="select *#(lf)from table_name#(lf)where transaction_date >= '" & DateTime.ToText(current_date, [Format="yyyy-MM-dd HH:mm:ss", Culture="en-US"]) & "'"])
in
Source
Note, that every time when the parameter value changes to something new, the user will be prompted to confirm the query.请注意,每次参数值更改为新内容时,都会提示用户确认查询。 To turn this off, you must go to settings and uncheck
Require user approval for native database queries
checkbox:要关闭此功能,您必须 go 进行设置并取消选中
Require user approval for native database queries
复选框:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.