使用ChatGPT生成Power BI DAX说明

同事的一句话印象特别深刻,最讨厌两件事:一是别人不写文档;二是自己要写文档。嗯,其实,我也是这样,虽然一直在部门里推文档留存,打造部门的知识库,但真的很讨厌写文档,特别是写度量值的说明文档。

但是,现在我们可以交给AI来写啊,先来问一个简单的,(这里使用的是Search · chatbox — 搜索 · 聊天框 (github.com), 需要使用自己的Key来使用,还好先前注册的账号还有18美元的额度可以用来调用API,如果有VISA信用卡的朋友也可以申请Azure的试用,目前可以免费使用一年chatgpt3.5)

现在,问题又来了,一个个来chatgpt中翻译也太麻烦了,能不能使用脚本来批量生成呢?

然而,chatgpt给我的这个库并没有连接本地文件的这个功能,那只能通过其他方法了,在之前有写过PowerBI使用Tabular Editor翻译报表模型,当然也可以调用接口来添加度量说明吧,还是直接问chatgpt吧

很遗憾,这段代码有语法错误,而我又不会c#代码,懒得去折腾,既然方法是行的通的,那就github上搜索别人的代码

Update Tabular Model Descriptions from ChatGPT with rate limiting logic (github.com)

#r "System.Net.Http"
using System.Net.Http;
using System.Text;
using Newtonsoft.Json.Linq;

// You need to signin to https://platform.openai.com/ and create an API key for your profile then paste that key 
// into the apiKey constant below
const string apiKey = "自己的key";
const string uri = "https://api.openai.com/v1/completions";
const string question = "解释下面的度量,但不要使用函数名称:\n\n";

using (var client = new HttpClient()) {
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);

    foreach (var t in Model.Tables)
    {
        foreach ( var m in t.Measures)
        {
            // Only uncomment the following when running from the command line or the script will 
            // show a popup after each measure
            //Info("Processing " + m.DaxObjectFullName) 
            //var body = new requestBody() { prompt = question + m.Expression   };
            var body = 
                "{ \"prompt\": " + JsonConvert.SerializeObject( question + m.Expression ) + 
                ",\"model\": \"text-davinci-003\" " +
                ",\"temperature\": 1 " +
                ",\"max_tokens\": 2048 " +
                ",\"stop\": \".\" }";

            var res = client.PostAsync(uri, new StringContent(body, Encoding.UTF8,"application/json"));
            res.Result.EnsureSuccessStatusCode();
            var result = res.Result.Content.ReadAsStringAsync().Result;
            var obj = JObject.Parse(result);
            var desc = obj["choices"][0]["text"].ToString().Trim();
            m.Description = desc + "\n=====\n" + m.Expression;
        }

    }
}

可以看下效果,这里要注意调用接口是有频率限制的,而且每次生成结果也会不同

有了度量值说明,之后就使用DMV语句查询所有度量的说明并导出了,当然也可以使用现在的外部工具,感兴趣可以阅读

用报表说明PowerBI报表

PowerBI 中使用DMV获取数据

DMV 语句

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注