Forum Discussion
Prompts to Power BI using Python scripts
Hi, I am trying to compare software version in 2 columns and return a third column based on the results and in order to do this I am using Python. The script works perfectly fine in Jupyter notebook but it does not works in PowerBi. Can anyone help me with the issue please. I am attaching screenshot of my data and error. the script is also attached below:
import pandas as pd
import numpy as np
v1 = dataset['Latest Packaged Version'].fillna('').tolist()
v2 = dataset['Version'].fillna('').tolist()
def converter_list(version_list):
v12 = list()
for i in version_list:
temp = i.replace(".", "")
temp1 = ""
zero_flag = 0
for j in reversed(range(0, len(temp))):
if(temp[j] == '0'):
zero_flag = 1
temp1 = temp[j]
else:
break
if(zero_flag):
temp1 = temp
if(temp1 == ""):
temp1 = "0"
v12.append(int(temp1))
return (v12)
def compare_list(list1, list2):
temp_list = list()
for i, j in zip(list1, list2):
if i >= j:
temp_list.append(False)
else:
temp_list.append(True)
return temp_list
v1_int = converter_list(v1)
v2_int = converter_list(v2)
version_check_list = compare_list(v1_int, v2_int)
dataset['res'] = version_check_list
Anonymous ,
Please import the csv to power bi at first. Then implement the python script. The script works well on my side.
Community Support Team _ Jimmy Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
8 Replies
- v-yuta-msftCommunity Support
Anonymous ,
I have made a test on my side, but your python script works well on my side. I have formatted your code as below:
# 'dataset' holds the input data for this script import pandas as pd v1 = dataset['Latest Packaged Version'].fillna('').tolist() v2 = dataset['Version'].fillna('').tolist() def converter_list(version_list): v12 = list() for i in version_list: temp = i.replace(".", "") temp1 = "" zero_flag = 0 for j in reversed(range(0, len(temp))): if(temp[j] == '0'): zero_flag = 1 temp1 = temp[j] else: break if(zero_flag): temp1 = temp if(temp1 == ""): temp1 = "0" v12.append(int(temp1)) return (v12) def compare_list(list1, list2): temp_list = list() for i, j in zip(list1, list2): if i >= j: temp_list.append(False) else: temp_list.append(True) return temp_list v1_int = converter_list(v1) v2_int = converter_list(v2) version_check_list = compare_list(v1_int, v2_int) dataset['res'] = pd.Series(version_check_list)So if your issue still persists, I think it's related to your data source. Please check if the value in [Version] or [Latest Packaged Version] are text type after imported in power bi.
Community Support Team _ Jimmy Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- AnonymousNot applicable
Thanks a lot for the prompt revert. I wasted so much time to figure this out but all went in vain. I have made changes to the script. Could you please help me with the new script? i would be very thankful to you.
Also, when I tried executing your code it gave me the result attached with this message.
import pandas as pd
v1 = dataset['Latest Packaged Version'].fillna('').tolist()
v2 = dataset['Version'].fillna('').tolist()def change_string_to_ascii(v12):
fin_list = []
for i in v12:
if(i.isdigit()):
fin_list.append(i)
else:
#print(i)
fin_list.append((ord(i))
#fin_str = "".join(fin_list)
return fin_list
def compare_list(list1, list2):
result_list = []
for i,j in zip(list1, list2):
if (i == "" or j == ""):
if(i == ""):
result_list.append("Upgraded")
else:
result_list.append("Not Upgraded")
continue
if(len(i) > len(j)):
result_list.append("Not Upgraded")
continue
elif(len(i)< len(j)):
result_list.append("Upgraded")
continue
else:
i_arr = i.split(".")
j_arr = j.split(".")
i_arr_dl = change_string_to_ascii(i_arr)
j_arr_dl = change_string_to_ascii(j_arr)
count = 0
for i_val, j_val in zip(i_arr_dl, j_arr_dl):
count+=1
if(i_val > j_val):
result_list.append("Not Upgraded")
break
elif(i_val == j_val):
#print(count, len(i_arr))
if(count == len(i_arr)):
#print("added")
result_list.append("Upgraded")
else:
continue
elif(i_val < j_val):
result_list.append("Upgraded")
break
return result_listversion_check_list = compare_list(v1, v2)
dataset['res'] = pd.Series(version_check_list)
- v-yuta-msftCommunity Support
Anonymous ,
What's the issue? Could you clarify more details about the issue or requirement and share some sample data/sample file?
Regards,
Jimmy Tao