What would be a correct PowerQuery syntax to extract the information from this Web JSON into a table:
I'm not very familiar with PowerQuery, and this is probably the only time I'll need this, so I'd be grateful if someone would help me out without refering me to documentation. Thanks
[{"time_entry_group": {"minutes": 301,"time_entries_params": {"locked": "0","from": "2021-02-01","to": "2021-02-28","customer_id": "11223344","project_id": "223388","service_id": "435248"},"revenue": 57691.6666666667,"project_id": 223388,"project_name": "Scrb","service_id": 435248,"service_name": "Meetings","month": "202102"}}
, {"time_entry_group": {"minutes": 1175,"time_entries_params": {"locked": "1","from": "2021-01-01","to": "2021-01-31","customer_id": "11223344","project_id": "223388","service_id": "421393"},"revenue": 225208.333333333,"project_id": 223388,"project_name": "Scrb","service_id": 421393,"service_name": "Design","month": "202101"}}
, {"time_entry_group": {"minutes": 24,"time_entries_params": {"locked": "1","from": "2021-01-01","to": "2021-01-31","customer_id": "11223344","project_id": "3168911","service_id": "95033"},"revenue": 4600.0,"project_id": 3168911,"project_name": "youkn Dev","service_id": 95033,"service_name": "Reviews","month": "202101"}}]
For future reference, if you have a column that you need to expand, you can instead click this arrow icon to the right of the column name. Clicking it should display a menu that should then allow you to specify which nested columns you want to get expand or get at. To be clear, it will expand that column for all rows in that table, not just one.
The JSON you've included is basically an array of objects, so maybe use:
Json.Document to parse the JSON, which should give you a list of records
Table.FromRecords to turn the list of records into a table.
Table.ExpandRecordColumn to expand a nested record columns.
Example implementation:
let
json = "[{""time_entry_group"":{""minutes"":301,""time_entries_params"":{""locked"":""0"",""from"":""2021-02-01"",""to"":""2021-02-28"",""customer_id"":""11223344"",""project_id"":""223388"",""service_id"":""435248""},""revenue"":57691.6666666667,""project_id"":223388,""project_name"":""Scrb"",""service_id"":435248,""service_name"":""Meetings"",""month"":""202102""}},{""time_entry_group"":{""minutes"":1175,""time_entries_params"":{""locked"":""1"",""from"":""2021-01-01"",""to"":""2021-01-31"",""customer_id"":""11223344"",""project_id"":""223388"",""service_id"":""421393""},""revenue"":225208.333333333,""project_id"":223388,""project_name"":""Scrb"",""service_id"":421393,""service_name"":""Design"",""month"":""202101""}},{""time_entry_group"":{""minutes"":24,""time_entries_params"":{""locked"":""1"",""from"":""2021-01-01"",""to"":""2021-01-31"",""customer_id"":""11223344"",""project_id"":""3168911"",""service_id"":""95033""},""revenue"":4600,""project_id"":3168911,""project_name"":""youkn Dev"",""service_id"":95033,""service_name"":""Reviews"",""month"":""202101""}}]",
parsed = Json.Document(json),
initialTable = Table.FromRecords(List.Transform(parsed, each [time_entry_group])),
expanded = Table.ExpandRecordColumn(initialTable, "time_entries_params", {"locked", "from", "to", "customer_id"})
in
expanded
One thing about the code above is that it doesn't expand nested fields project_id and service_id (present within time_entries_params). This is because these columns already exist in the table (and having duplicate column names would cause an error). I've assumed this isn't a problem, as the nested values aren't different.
How to convert TIBQuery to TIBTable, additionally to display it in DBGrib?
I want to sort data in DBGrid and I used TIBQuery to take sort data from database, and I have problem to convert data from TIBQuery to TIBTable.
Create a VCL C++Builder app - Drop in TIBDatabase, TIBTable, TDataSource and TDBGrid on your form. Make active the TIBDatabase and TIBTable connections. Right mouse click on the TIBTable to add the columns you want to have appear in your TDBGrid. Leave the Column Header text to be the same as the Column Names.
Add the following line of code to the onTitleClick for the DBGrid:
void __fastcall TForm1::DBGrid1TitleClick(TColumn *Column)
{
// set TIBTable's IndexFieldNames property to the column title field name
// this will sort ascending all of the data in the DBGrid
IBTable1->IndexFieldNames = Column->FieldName;
}
If you want to do even more, I suggest that you add a TDataSetProvider and TClientDataSet to your form and then you can do more with your app.
I'm using a QtableView to display and edit data from a QsqlTableModel.
Everything's fine : data from a postgreSQL table is displayed, and user can edit it and save modifications.
I want to add a row when uses click on a button. I use the insertRecord method from my QslTableModel.
The row is correctly added in the QTableView.
My problem is :
I want to insert a value from a query in the first cell of this new row. (to automatically populate an unique identifier).
This is my code :
def ajoutlgn(self):
query_idNewLine = QtSql.QSqlQuery(self.db)
if query_idNewLine.exec_('SELECT sp_idsuivi+1 FROM bdsuivis.t_suivprev_tablo ORDER BY sp_idsuivi DESC LIMIT 1'):
while query_idNewLine.next():
identifiant = query_idNewLine.value(0)
#print identifiant
record = QtSql.QSqlRecord()
record.setValue(1,identifiant)
self.model.insertRecord(self.model.rowCount(), record)
The value is not inserted in the new row (but if I enter a value by hand, it works perfectly).
Nevertheless, the query is OK (as I can see with the « print identifiant » line, which returns the expected integer).
Do you see what I'm doing wrong ?
Are there other methods to insert programmatically a value in a QTableView cell?
Or do I have to use a QitemDelegate ?
Thanks for advance.
Finally I found the solution :
This line creates a record, but not a record for my QsqlTableModel
record = QtSql.QSqlRecord()
I replaced it with this one, and it works perfectly :
record = self.model.record()
Is there a way I can use a DataColumn from a DataTable as the ValueList for an UltraGrid column?
I have the column style set to DropDownList. The question is, what do I use for ValueList?
ultraGrid1.DisplayLayout.Bands[0].Columns["col"].Style = ColumnStyle.DropDownList;
ultraGrid1.DisplayLayout.Bands[0].Columns["col"].ValueList = ???
Create a BindableValueList. One of the Constructors takes the object for the datasource, the dataMember, the displayMember, valueMember, and bindingContextControl.
The line of code to set the ValueList:
this.ultraGrid1.DisplayLayout.Bands[0].Columns["col"].ValueList = new BindableValueList(dt, "", "displayColumn","valueColumn", this.ultraGrid1);
In the above example, dt is the DataTable you are binding to, displayColumn and valueColumn would be the keys for the columns that you want to use as the display and value portions of the drop down. If you are binding to a DataSet and want to bind to a table other than the first table you would use the second parameter to pass in the name of the table to bind to.
This could be of help. It a simple procedure that receives a DataTable, the value for the Key property of the ValueList and the name of the field to use as list for the the ValueList and returns a Value list to set in one of your UltraGridColums
public ValueList DataTableToValueList(DataTable dt, string vlKey, string fieldName)
{
ValueList vl = new ValueList();
if (!string.IsNullOrEmpty(vlKey)) vl.Key = vlKey;
foreach (DataRowView r in dt.DefaultView)
vl.ValueListItems.Add(r[fieldName]);
return vl;
}
use in this way (probably in the InitializeLayout event)
grd.DisplayLayout.Bands[0].Columns["col"].ValueList =
DataTableToValueList(dtCustomers,"vlCustomer", "CustomerName");
Say I have a QTableWidget with 10 columns. I set data in the table with
QTableWidgetItem* textItem1 = new QTableWidgetItem;
textItem1->setData(Qt::DisplayRole, 20);
this->tableWidget->setItem(row, col, textItem1);
The problem is that if I change the order or titles of the columns in the table using QtDesigner, I have to go through my code and change all of the column numbers. I would rather do something like this:
QTableWidgetItem* textItem1 = new QTableWidgetItem;
textItem1->setData(Qt::DisplayRole, "Smith");
this->tableWidget->setLastName(row, textItem1);
to add "Smith" to the "LastName" column, without needing to know which column index LastName currently is. Is the way to do this with a custom Model? I looked into QAbstractTableModel, but I don't understand how to provide named access to data? Can anyone briefly explain how one would do this? And if this is a reasonable/common thing to want to do?
Thanks.
I posted an example of using QAbstractTableModel:
http://programmingexamples.net/index.php?title=Qt/ModelView/QAbstractTableModel
As you can see, the data is simply stored in a member variable, and can therefore be accessed however you would like.